Search code examples
pythonpandasmulti-index

pandas construct multi index for columns


How can I construct a multi-index in pandas for an example dataframe of:

import pandas as pd
df = pd.DataFrame({'day':['2020-01-01', '2020-01-02'], 'value_mean':[1,5], 'value_max':[40,100]})

Transform the existing:

          day  value_mean  value_max
0  2020-01-01           1         40
1  2020-01-02           5        100

To something like:

                            value
          day         mean        max
0  2020-01-01           1         40
1  2020-01-02           5        100

Solution

  • There is problem join no Multiindex with MultiIndex columns, only trick should be use empty strings for second level:

    df.columns = df.columns.str.split('_', expand=True)
    df = df.rename(columns = lambda x: x if pd.notna(x) else '')
    print (df)
              day value     
                   mean  max
    0  2020-01-01     1   40
    1  2020-01-02     5  100
    
    print (df.columns)
    MultiIndex([(  'day',     ''),
                ('value', 'mean'),
                ('value',  'max')],
               )
    

    If want clean, not empty strings values in levels:

    df = df.set_index('day')
    df.columns = df.columns.str.split('_', expand=True)
    print (df)
               value     
                mean  max
    day                  
    2020-01-01     1   40
    2020-01-02     5  100