Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数据帧获取索引与特定条件匹配的行_Python_Pandas_Dataframe - Fatal编程技术网

Python 数据帧获取索引与特定条件匹配的行

Python 数据帧获取索引与特定条件匹配的行,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧,从中我需要索引匹配特定条件的所有行。数据帧有一个多索引,我需要第一个索引TimeStamp在特定范围内的行。多索引的级别1是一系列DateTime对象。以下代码行用于检查月份是否等于5: compare[compare.index.get_level_values(0).month == 5] 但是当我修改代码以检查值位于某个数组中的行时 compare[compare.index.get_level_values(0).month in [5, 6, 7]] 我得到了错误

我有一个数据帧,从中我需要索引匹配特定条件的所有行。数据帧有一个多索引,我需要第一个索引
TimeStamp
在特定范围内的行。多索引的级别1是一系列DateTime对象。以下代码行用于检查月份是否等于5:

compare[compare.index.get_level_values(0).month == 5]
但是当我修改代码以检查值位于某个数组中的行时

compare[compare.index.get_level_values(0).month in [5, 6, 7]]
我得到了错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我还尝试使用
df.loc
获取值

compare.loc[compare.index.get_level_values(0).month in [5, 6, 7]]
但这会导致同样的错误

我还尝试使用
isin
方法

compare[compare.index.get_level_values(0).month.isin([5, 6, 7])]
但这会导致以下属性错误:

AttributeError: 'numpy.ndarray' object has no attribute 'isin'
如何获取索引满足特定条件的数据帧行?

尝试以下方法:

compare[compare.index.get_level_values(0).month.isin([5, 6, 7])]
PS

演示:

更新:使用索引值进行测试:

In [56]: i = pd.DatetimeIndex(['2016-01-04 01:40:00', '2016-01-04 02:00:00', '2016-01-04 02:10:00', '2016-01-04 02:30:00', '2016-01-04 02:4
    ...: 0:00'], dtype='datetime64[ns]', name=u'TTimeStamp', freq=None)

In [57]: i
Out[57]: DatetimeIndex(['2016-01-04 01:40:00', '2016-01-04 02:00:00', '2016-01-04 02:10:00', '2016-01-04 02:30:00', '2016-01-04 02:40:00'],
dtype='datetime64[ns]', name='TTimeStamp', freq=None)

In [58]: i.month
Out[58]: Int64Index([1, 1, 1, 1, 1], dtype='int64', name='TTimeStamp')

In [59]: i.month.isin([2,3])
Out[59]: array([False, False, False, False, False], dtype=bool)

In [60]: i.month.isin([1,2,3])
Out[60]: array([ True,  True,  True,  True,  True], dtype=bool)
更新2:尝试以下解决方法:

compare[pd.Series(compare.index.get_level_values(0).month).isin([5, 6, 7]).values]

对不起,我忘了补充一下,我也试过了,但没用…问题是用我得到的错误编辑的too@victor,您能提供一个小的可复制数据集吗?我创建了
DatetimeIndex
,并称为
I.month
。它不是创建一个
int54索引
,而是创建一个数据类型为
int32
ndarray
。这似乎是错误的根源,对
month
的调用创建了
ndarray
而不是
Index
对象。我检查了原始索引,同样的事情发生了——包括
month
属性会导致
DatetimeIndex
转换为
ndarray
。不过,我还没有找到解决办法。好吧,这很有效!谢谢你的解决方案!我仍然很好奇为什么
ndarray
是从
DatetimeIndex
对象创建的——如果我知道原因,我会继续四处查看,并在这里发布。你能发布
print(compare.index.get_level_values(0)[:5])的输出吗?你的熊猫版本是什么?
DatetimeIndex(['2016-01-04 01:40:00','2016-01-04 02:00:00','2016-01-04 02:10:00','2016-01-04 02:30:00','2016-01-04 02:40:00',dtype='datetime64[ns]',name=u'TTimeStamp',freq=None)
是我从
print
语句中得到的输出。我正在使用Pandas 0.20.3请参见我答案中的更新-我无法重现您的错误。。。我用的是熊猫0.20.1
compare[pd.Series(compare.index.get_level_values(0).month).isin([5, 6, 7]).values]