Pandas - How to flatten a hierarchical index in columns

后端 未结 17 1067
忘掉有多难
忘掉有多难 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:16

    To flatten a MultiIndex inside a chain of other DataFrame methods, define a function like this:

    def flatten_index(df):
      df_copy = df.copy()
      df_copy.columns = ['_'.join(col).rstrip('_') for col in df_copy.columns.values]
      return df_copy.reset_index()
    

    Then use the pipe method to apply this function in the chain of DataFrame methods, after groupby and agg but before any other methods in the chain:

    my_df \
      .groupby('group') \
      .agg({'value': ['count']}) \
      .pipe(flatten_index) \
      .sort_values('value_count')
    

提交回复
热议问题