堆叠MultiIndex DataFrame并合并索引

时间:2016-12-30 13:09:14

标签: python pandas dataframe multi-index

我的DataFrame看起来像这样:

                       00:00  01:00  02:00  03:00  04:00  05:00  06:00  07:00
Code Alias Date                                                                 
RO   FI    05.07.2010  53.97  52.11  52.11  52.11  52.11  52.11  51.85  51.55   
     JY    05.07.2010  53.97  52.11  52.11  52.11  52.11  52.11  51.85  65.85   
     SE    05.07.2010  53.97  52.11  52.11  52.11  52.11  52.11  51.85  51.55   
     SJ    05.07.2010  53.97  52.11  52.11  52.11  51.49  52.11  51.85  51.55   

使用df.stack()时,我将列(小时)作为单独的索引。如何合并'日期'索引和新的“小时”#39; index形成DateTime索引?

像这样:

                             Value
Code Alias Date                                                                 
RO   FI    05.07.2010 00:00  53.97  
           05.07.2010 01:00  52.11
           05.07.2010 02:00  52.11

1 个答案:

答案 0 :(得分:1)

它不漂亮,但它会做

  • 使用strftimeDate转换为字符串并与Hour
  • 连接
  • 然后转到pd.to_datetime
s = df.rename_axis('Hour', 1).stack()

d1 = s.reset_index(['Date', 'Hour'], name='Value')

d1.set_index(
    pd.to_datetime(
        d1.Date.dt.strftime('%Y-%m-%d ') +
        d1.Hour
    ), append=True
).Value

Code  Alias                     
RO    FI     2010-05-07 00:00:00    53.97
             2010-05-07 01:00:00    52.11
             2010-05-07 02:00:00    52.11
             2010-05-07 03:00:00    52.11
             2010-05-07 04:00:00    52.11
             2010-05-07 05:00:00    52.11
             2010-05-07 06:00:00    51.85
             2010-05-07 07:00:00    51.55
      JY     2010-05-07 00:00:00    53.97
             2010-05-07 01:00:00    52.11
             2010-05-07 02:00:00    52.11
             2010-05-07 03:00:00    52.11
             2010-05-07 04:00:00    52.11
             2010-05-07 05:00:00    52.11
             2010-05-07 06:00:00    51.85
             2010-05-07 07:00:00    65.85
      SE     2010-05-07 00:00:00    53.97
             2010-05-07 01:00:00    52.11
             2010-05-07 02:00:00    52.11
             2010-05-07 03:00:00    52.11
             2010-05-07 04:00:00    52.11
             2010-05-07 05:00:00    52.11
             2010-05-07 06:00:00    51.85
             2010-05-07 07:00:00    51.55
      SJ     2010-05-07 00:00:00    53.97
             2010-05-07 01:00:00    52.11
             2010-05-07 02:00:00    52.11
             2010-05-07 03:00:00    52.11
             2010-05-07 04:00:00    51.49
             2010-05-07 05:00:00    52.11
             2010-05-07 06:00:00    51.85
             2010-05-07 07:00:00    51.55
Name: Value, dtype: object