Search code examples
pythonpandasmulti-indexmulti-level

How do I add a multi-level column index to an existing df?


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    |  

Solution

  • You can manually construct a pandas.MultiIndex using one of several constructors. From the docs for your case:

    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    |