Pandas - How to flatten a hierarchical index in columns

后端 未结 17 995
忘掉有多难
忘掉有多难 2020-11-22 02:55

I have a data frame with a hierarchical index in axis 1 (columns) (from a groupby.agg operation):

     USAF   WBAN  year  month  day  s_PC  s_CL         


        
17条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 03:08

    And if you want to retain any of the aggregation info from the second level of the multiindex you can try this:

    In [1]: new_cols = [''.join(t) for t in df.columns]
    Out[1]:
    ['USAF',
     'WBAN',
     'day',
     'month',
     's_CDsum',
     's_CLsum',
     's_CNTsum',
     's_PCsum',
     'tempfamax',
     'tempfamin',
     'year']
    
    In [2]: df.columns = new_cols
    

提交回复
热议问题