索引pandas.Panel违反直觉或错误?

时间:2014-11-04 13:47:53

标签: python pandas

我在理解pandas.Panel(3D数据结构)的索引方面遇到了一些麻烦。在documentation中,声明索引的工作原理如下:

  

从具有多轴选择的对象获取值使用以下表示法(使用.loc作为示例,但也适用于.iloc和.ix)。任何轴访问器都可以是空切片:。假设不在规范中的轴是:。 (例如p.loc ['a']等于p.loc ['a',:,]])

     

p.loc [item_indexer,major_indexer,minor_indexer]

现在我假设在提取DataFrame时其余索引的顺序不会改变,但是:

from pandas import Panel
from numpy import arange
p = Panel(arange(24).reshape(2,3,4))
p.shape
Out[4]: (2, 3, 4)
p.iloc[0].shape # original order
Out[5]: (3, 4)
p.iloc[:,0].shape # transposed
Out[6]: (4, 2)
p.iloc[:,:,0].shape # transposed
Out[7]: (3, 2)
p.iloc[:,0,:].shape # transpose (same as [6])
Out[8]: (4, 2)
p.iloc[1:,0,:].shape # Slicing item_indexer, then transpose
Out[9]: (4, 1)
p.iloc[1:,0].shape # Expected to get the same as [9], but slicing minor_indexer instead????
Out[10]: (3, 2)

在索引major_index或minor_index而不是item_index时,为什么要转换DataFrame?为什么最后一个例子与之前的例子不同?​​

Link to github issue

1 个答案:

答案 0 :(得分:1)

最近在github issue进行了一些讨论。看起来大熊猫不太适合N维数据(N> = 3)。有一个替代模块xray,它类似于熊猫,但对于ND数据。否则,您可以使用带有MultiIndex的普通pandas(2D)DataFrame来模拟ND数据。