使用Pandas Panel获取错误布尔索引

时间:2017-03-16 23:29:52

标签: pandas indexing panel

我有一个面板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 个答案:

答案 0 :(得分: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']))]