2017-03-16 81 views
1

我有一個面板stocks具有['volume'], ['30_Avg_Vol'] and ['20_Avg_Vol']作爲軸掌握大熊貓面板布爾索引錯誤

stocks.Volume 
        AAPL  OPK 
Date        
010-01-11 115557365.0 130195.0 
2010-01-12 148614774.0 121245.0 
2010-01-13 151472335.0 47897.0 
2010-01-14 108288411.0 126539.0 
2010-01-15 148584065.0 175261.0 
2010-01-19 182501620.0 123318.0 
2010-01-20 153037892.0 100803.0 
2010-01-21 152038565.0 262285.0 
2010-01-22 220441872.0 114525.0 


stocks['30_Avg_Vol'] 
Date    AAPL   OPK 
2010-01-21   NaN   NaN 
2010-01-22   NaN   NaN 
2010-01-25 1.526774e+08 1.607402e+05 
2010-01-26 1.723086e+08 1.592968e+05 
2010-01-27 1.875047e+08 1.554605e+05 
2010-01-28 1.933864e+08 1.540021e+05 
2010-01-29 1.996023e+08 1.549712e+05 
2010-02-01 1.989956e+08 1.532240e+05 

stocks['20_Avg_Vol'] 
        AAPL   OPK 
Date       
2010-01-12   NaN   NaN 
2010-01-13   NaN   NaN 
2010-01-14   NaN   NaN 
2010-01-15 1.315716e+08 1.718937e+05 
2010-01-19 1.362016e+08 1.674777e+05 
2010-01-20 1.376046e+08 1.619215e+05 
2010-01-21 1.387149e+08 1.696418e+05 
2010-01-22 1.445526e+08 1.657049e+05 
2010-01-25 1.526774e+08 1.607402e+05 
2010-01-26 1.723086e+08 1.592968e+05 
2010-01-27 1.875047e+08 1.554605e+05 

我想裁板以下列方式

stocks[(stocks['Volume']> (1.5 * stocks['30_Avg_Vol']))|(stocks['Volume']> (1.5* stocks['20_Avg_Vol']))] 

無論中,成交量大於平均交易量30天或平均交易量20天的1.5倍。

,但我發現了以下錯誤:

raise ValueError('Cannot index with multidimensional key') 
ValueError: Cannot index with multidimensional key 

圍繞這個辦法嗎?

謝謝。

回答

1

您有MultiIndex。要索引到MultiIndex,您必須成對指定您的密鑰(或以三元組形式,等等)。

來解決這個問題最簡單的辦法是重置到一個單一的指標:

所以:

stocks[(stocks['Volume']> (1.5 * stocks['30_Avg_Vol']))|(stocks['Volume']> (1.5* stocks['20_Avg_Vol']))] 

變爲:

_stocks = stocks.reset_index() 

_stocks[(_stocks['Volume']> (1.5 * _stocks['30_Avg_Vol']))|(_stocks['Volume']> (1.5* _stocks['20_Avg_Vol']))] 
+0

感謝您的答覆。我無法測試代碼,但會回覆一次。 =) – Moondra

+0

所以,當我運行這個代碼:'_stocks = stocks.reset_index()'我得到以下錯誤:'AttributeError:'面板'對象沒有屬性'reset_index' 下劃線如果股票正面,那樣做?謝謝。 – Moondra

+1

啊,你正在使用面板;對不起,我錯過了你的總結。如果這不起作用,我實際上建議將'pandas'中的'pandas'做爲'*',因爲'pandas'' Panel'對象被棄用,IIRC將在下一個主要版本中完全刪除('0.20 .0')。相反,請使用['xarray'](http://xarray.pydata.org/en/stable/),它旨在取代'pandas.Panel'。比照http://xarray.pydata.org/en/stable/indexing.html。這不是直接回答你的問題,雖然不幸... –