1

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

Another option:

df.columns = pd.MultiIndex(levels=[['value', 'day'], ['mean','max','']], codes=[[1,0,0],[2,0,1]])

Output:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.