如何将refdex_axis Pandas Panel转换为MultiIndex

时间:2013-09-06 06:16:30

标签: python pandas panel multi-index

我有一个3D面板数据。 我无法将其重新索引到2级的多索引。

我创建了多索引'mind'。

import pandas as pd

mind = pd.MultiIndex.from_arrays([['Consumer,Cyclical','Industrial','Software'], ['Retail','MiscellaneousManufactur','Technology'], ['AZO','AZZ','AZPN']],names=['sec','sub','ticker'])

dfclose = pd.DataFrame([[1.1,2.1,3.1],[1.2,2.2,3.2]], index=['2013-09-02','2013-09-03'], columns=['AZO','AZZ','AZPN'])
dfmean = dfclose - dfclose.mean()

pdata2 = pd.Panel({'close':dfclose, 'mean':dfmean})
pdata2.minor_axis.name='ticker'
pdata3=pdata2.reindex_axis(mind,axis=2,level='ticker')

但pdata3没有映射到新的多索引并给出NaN。

1 个答案:

答案 0 :(得分:1)

这似乎是0.12中的错误(并将在0.13中修复) 解决方法不是重新索引,而是在创建dfclose时使用MultiIndex:

dfclose = pd.DataFrame([[1.1, 2.1, 3.1], [1.2, 2.2, 3.2]],
                           index=['2013-09-02','2013-09-03'],
                           columns=mind)
dfmean = dfclose - dfclose.mean()
pdata2 = pd.Panel({'close':dfclose, 'mean':dfmean})
pdata2.minor_axis.name='ticker'

In [11]: pdata2.iloc[0]
Out[12]: 
sec         Consumer,Cyclical               Industrial    Software
sub                    Retail  MiscellaneousManufactur  Technology
ticker                    AZO                      AZZ        AZPN
2013-09-02                1.1                      2.1         3.1
2013-09-03                1.2                      2.2         3.2

另一种选择是使用DataFrame:

In [12]: pd.concat([dfmean, dfclose], axis=1, keys=['dfmean' ,'dfclose'])
Out[12]: 
                       dfmean                                                 dfclose                                       
sec         Consumer,Cyclical               Industrial    Software  Consumer,Cyclical               Industrial    Software 
sub                    Retail  MiscellaneousManufactur  Technology             Retail  MiscellaneousManufactur  Technology  
ticker                    AZO                      AZZ        AZPN                AZO                      AZZ        AZPN 
2013-09-02              -0.05                    -0.05       -0.05                1.1                      2.1         3.1  
2013-09-03               0.05                     0.05        0.05                1.2                      2.2         3.2