使用索引值访问 Pandas Data Frame 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37537343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access Pandas Data Frame row with index value
提问by Philippe Remy
I have a very simple Pandas Data Frame with one index (of type TimedeltaIndex
) and one column named TotalVolume
.
我有一个非常简单的 Pandas 数据框,有一个索引(类型TimedeltaIndex
)和一个名为TotalVolume
.
>> print(df)
TotalVolume
...
09:00:00 143846.153846
09:05:00 84353.846154
09:10:00 46946.153846
09:15:00 46765.384615
09:20:00 53076.923077
09:25:00 31642.307692
09:30:00 48269.230769
...
I would like to be able to query this dictionary with 09:00:00
for example and get 143846.153846
. For information this is the structure of the index:
我希望能够使用09:00:00
例如查询这本字典并获取143846.153846
. 有关信息,这是索引的结构:
>> print(df.index)
TimedeltaIndex(['07:00:00', '07:05:00', '07:10:00', '07:15:00', '07:20:00', '07:25:00', '07:30:00', '07:35:00', '07:40:00', '07:45:00', '07:50:00', '07:55:00', '08:00:00', '08:05:00', '08:10:00', '08:15:00', '08:20:00', '08:25:00', '08:30:00', '08:35:00', '08:40:00', '08:45:00', '08:50:00', '08:55:00', '09:00:00', '09:05:00', '09:10:00', '09:15:00', '09:20:00', '09:25:00', '09:30:00', '09:35:00', '09:40:00', '09:45:00', '09:50:00', '09:55:00', '10:00:00', '10:05:00', '10:10:00', '10:15:00', '10:20:00', '10:25:00', '10:30:00', '10:35:00', '10:40:00', '10:45:00', '10:50:00', '10:55:00', '11:00:00', '11:05:00', '11:10:00', '11:15:00', '11:20:00', '11:25:00', '11:30:00', '11:35:00', '11:40:00', '11:45:00', '11:50:00', '11:55:00', '12:00:00', '12:05:00', '12:10:00', '12:15:00', '12:20:00', '12:25:00', '12:30:00', '12:35:00', '12:40:00', '12:45:00', '12:50:00', '12:55:00', '13:00:00', '13:05:00', '13:10:00', '13:15:00', '13:20:00', '13:25:00', '13:30:00', '13:35:00', '13:40:00', '13:45:00',
'13:50:00', '13:55:00', '14:00:00', '14:05:00', '14:10:00', '14:15:00', '14:20:00', '14:25:00', '14:30:00', '14:35:00', '14:40:00', '14:45:00', '14:50:00', '14:55:00', '15:00:00'],
dtype='timedelta64[ns]', freq=None)
When I do,
当我做,
print(df['09:00:00'])
I have
我有
TotalVolume
09:00:00 143846.153846
09:05:00 84353.846154
09:10:00 46946.153846
09:15:00 46765.384615
09:20:00 53076.923077
09:25:00 31642.307692
09:30:00 48269.230769
09:35:00 35715.384615
09:40:00 38576.923077
09:45:00 37211.538462
09:50:00 41803.846154
09:55:00 37503.846154
It seems like the filter is not working as I would like. It works correctly for 09:05:00
though.
过滤器似乎无法正常工作。不过,它可以正常工作09:05:00
。
What is the most pandatonic way to do it?
最笨拙的方法是什么?
采纳答案by jezrael
For me works loc
:
对我来说有效loc
:
print (df)
TotalVolume
09:00:00 143846.153846
09:05:00 84353.846154
09:10:00 46946.153846
09:15:00 46765.384615
09:20:00 53076.923077
09:25:00 31642.307692
09:30:00 48269.230769
print (df.index)
TimedeltaIndex(['09:00:00', '09:05:00', '09:10:00', '09:15:00', '09:20:00',
'09:25:00', '09:30:00'],
dtype='timedelta64[ns]', freq=None)
print(df.loc['09:00:00', 'TotalVolume'])
143846.153846
print(df.loc['0 day 09:00:00', 'TotalVolume'])
143846.153846
print(df.loc['09:00:00'])
TotalVolume 143846.153846
Name: 0 days 09:00:00, dtype: float64
But:
但:
print(df['09:05:00'])
KeyError: '09:05:00'
密钥错误:'09:05:00'
And:
和:
print(df['09:05:00':'09:20:00'])
TotalVolume
09:05:00 84353.846154
09:10:00 46946.153846
09:15:00 46765.384615
09:20:00 53076.923077