2017-08-11 73 views
1

我有一個熊貓數據框,從中我需要索引與特定條件匹配的所有行。數據框有一個MultiIndex,我需要第一個索引TimeStamp在特定範圍內的行。 MultiIndex的第1級是一系列DateTime對象。這下面的代碼行的工作,以檢查是否一個月等於5:熊貓DataFrame獲取索引匹配一定條件的行

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' 

如何獲取其中指數滿足特定條件的數據框的行?

+0

你可以張貼的'打印輸出(compare.index.get_level_values(0)[5])'?你的Pandas版本是什麼? – MaxU

+0

'DatetimeIndex(['2016-01-04 01:40:00','2016-01-04 02:00:00','2016-01-04 02:10:00','2016-01-04 02:00:00','2016-01-04 02:40:00'],dtype ='datetime64 [ns]',name = u'TTimeStamp',freq = None)''打印'聲明。我正在使用熊貓0.20.3 – victor

+0

請在我的答案中看到更新 - 我不能重現你的錯誤...我使用熊貓0.20.1 – MaxU

回答

2

試試這個:

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

PS this should work for Pandas version 0.18.1+

演示:

In [45]: import pandas_datareader.data as web 

In [46]: df = web.DataReader('AAPL', 'google', '2017-06-01') 

In [48]: df = df.assign(i2=np.arange(len(df))).set_index('i2', append=True) 

In [49]: df 
Out[49]: 
       Open High  Low Close Volume 
Date  i2 
2017-06-01 0 153.17 153.33 152.22 153.18 16404088 
2017-06-02 1 153.58 155.45 152.89 155.45 27770715 
2017-06-05 2 154.34 154.45 153.46 153.93 25331662 
2017-06-06 3 153.90 155.81 153.78 154.45 26624926 
2017-06-07 4 155.02 155.98 154.48 155.37 21069647 
2017-06-08 5 155.25 155.54 154.40 154.99 21250798 
2017-06-09 6 155.19 155.19 146.02 148.98 64882657 
2017-06-12 7 145.74 146.09 142.51 145.42 72307330 
2017-06-13 8 147.16 147.45 145.15 146.59 34165445 
2017-06-14 9 147.50 147.50 143.84 145.16 31531232 
...    ...  ...  ...  ...  ... 
2017-07-31 41 149.90 150.33 148.13 148.73 19845920 
2017-08-01 42 149.10 150.22 148.41 150.05 35368645 
2017-08-02 43 159.28 159.75 156.16 157.14 69936800 
2017-08-03 44 157.05 157.21 155.02 155.57 27097296 
2017-08-04 45 156.07 157.40 155.69 156.39 20559852 
2017-08-07 46 157.06 158.92 156.67 158.81 21870321 
2017-08-08 47 158.60 161.83 158.27 160.08 36205896 
2017-08-09 48 159.26 161.27 159.11 161.06 26131530 
2017-08-10 49 159.90 160.00 154.63 155.32 40804273 
2017-08-11 50 156.60 158.57 156.07 157.48 26180743 

[51 rows x 5 columns] 

In [50]: df[df.index.get_level_values(0).month.isin([5,8])] 
Out[50]: 
       Open High  Low Close Volume 
Date  i2 
2017-08-01 42 149.10 150.22 148.41 150.05 35368645 
2017-08-02 43 159.28 159.75 156.16 157.14 69936800 
2017-08-03 44 157.05 157.21 155.02 155.57 27097296 
2017-08-04 45 156.07 157.40 155.69 156.39 20559852 
2017-08-07 46 157.06 158.92 156.67 158.81 21870321 
2017-08-08 47 158.60 161.83 158.27 160.08 36205896 
2017-08-09 48 159.26 161.27 159.11 161.06 26131530 
2017-08-10 49 159.90 160.00 154.63 155.32 40804273 
2017-08-11 50 156.60 158.57 156.07 157.48 26180743 

UPDATE:測試你的指數值:

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) 

UPDATE2:嘗試以下解決方法:

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

對不起,我忘了補充一點,我也試過,也沒有工作...問題編輯與我得到的錯誤 – victor

+0

@victor,你能提供一個小的可重複數據集? – MaxU

+0

我創建了'DatetimeIndex'並且調用了'i.month'。它不是創建一個'Int54Index',而是創建一個dtype爲int32的'ndarray'。看起來這是錯誤的根源,對'month'的調用創建一個'ndarray'而不是'Index'對象。我檢查了原始索引,發生了同樣的事情 - 包括'month'屬性導致'DatetimeIndex'被轉換爲'ndarray'。儘管如此,我還沒有找到解決方案。 – victor

相關問題