如何使用MultiIndex重新索引面板数据

时间:2018-08-28 03:28:40

标签: pandas dataframe panel

我获得了面板数据,如何在没有Multiindex的情况下获取数据框,我尝试这样做

print k_data
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 480 (major_axis) x 100 (minor_axis)
Items axis: close to volume
Major_axis axis: 2018-08-13 09:35:00 to 2018-08-24 15:00:00
Minor_axis axis: 603105.XSHG to 600236.XSHG

print k_data.to_frame()
                                  close    high     low        money    open  \
major               minor                                                      
2018-08-13 09:35:00 603105.XSHG   25.20   26.00   23.65  367025532.0   23.80   
                    300745.XSHE   56.85   56.88   56.03   27557052.0   56.47   
                    300746.XSHE   24.80   24.92   24.40   25316020.0   24.92   
                    300747.XSHE  156.77  157.01  155.11   74177868.0  155.67   
                    002932.XSHE   77.77   77.77   76.52   47234204.0   77.00   
                    603045.XSHG   45.48   45.49   45.00   12387785.0   45.00  

如何重新索引这样的列

major   minor   close   high    low money   open    volume
2018/8/3 9:31   603105.XSHG 24.2    24.44   24.2    75700508    24.3    3111000
2018/8/3 9:31   300745.XSHE 62.06   62.31   61.46   25664428    61.46   415385
2018/8/3 9:31   300746.XSHE 28.6    28.74   28.54   4479504 28.74   156300
2018/8/3 9:31   300747.XSHE 181.2   181.39  180.85  11388640    181.39  62900

1 个答案:

答案 0 :(得分:-1)

好的,我想出了问题

使用k_data.to_frame()。reset_index()进行重置,如下所示:

df = k_data.to_frame().reset_index()
print df
                    major        minor   close    high     low        money  \
0     2018-08-13 09:35:00  603105.XSHG   25.20   26.00   23.65  367025532.0   
1     2018-08-13 09:35:00  300745.XSHE   56.85   56.88   56.03   27557052.0   
2     2018-08-13 09:35:00  300746.XSHE   24.80   24.92   24.40   25316020.0   
3     2018-08-13 09:35:00  300747.XSHE  156.77  157.01  155.11   74177868.0   
4     2018-08-13 09:35:00  002932.XSHE   77.77   77.77   76.52   47234204.0   
5     2018-08-13 09:35:00  603045.XSHG   45.48   45.49   45.00   12387785.0   
6     2018-08-13 09:35:00  601330.XSHG   15.24   15.27   14.93   31972260.0