How do I add a multi-level column index to an existing df? I read in the df from an Excel file, so I don't want to re-create the df using pd.DataFrame.
Thank you
Current State
|Total Assets| AUMs |
Firm 1 | 100 | 300 |
Firm 2 | 200 | 3400 |
Firm 3 | 300 | 800 |
Firm 4 | NaN | 800 |
Desired State
Importance| H | H |
Category | Cat1 | Cat2 |
|Total Assets | AUMs |
Firm 1 | 100 | 300 |
Firm 2 | 200 | 3400 |
Firm 3 | 300 | 800 |
Firm 4 | NaN | 800 |
You can manually construct a pandas.MultiIndex
using one of several constructors. From the docs for your case:
MultiIndex.from_arrays
Convert list of arrays to MultiIndex.
MultiIndex.from_tuples
Convert list of tuples to a MultiIndex.
MultiIndex.from_frame
Make a MultiIndex from a DataFrame.
For your case, I think pd.MultiIndex.from_arrays
might be the easiest way:
df.columns=pd.MultiIndex.from_arrays([['H','H'],['Cat1','Cat2'],df.columns],names=['Importance','Category',''])
output:
Importance| H | H |
Category | Cat1 | Cat2 |
|Total Assets | AUMs |
Firm 1 | 100 | 300 |
Firm 2 | 200 | 3400 |
Firm 3 | 300 | 800 |
Firm 4 | NaN | 800 |