Search code examples
pythonpandasheadermulti-index

Add an additional level using MultiIndex.from_tuples in pandas


I am trying to add an extra level to my multi-level index in python. My data frame looks as following.

df1=pd.DataFrame(np.array([['A',1, 2, 3], ['B',-4,5 , -6], ['d',7, 0, 9]]), columns=['D','a', 'b', 'c'])

I used following code to add multi levels

df1.columns=pd.MultiIndex.from_tuples((("first","D"),("first",'a'),("second","b"),("second","c")))

Now, I would like to add another level between the first and second level from a different data frame

df2=pd.DataFrame(np.array([['D',1], ['a',2 ], ['b',1],['c',3]]), columns=['11','22'])

The desired output should be as following. I am wondering how I could achieve this. Thank you very much for your help in advance.

 first  second

 1  2   1  3

 D  a   b   c

A   1   2   3
B   -4  5   -6
d   7   0   9

Solution

  • Try:

    # cache the original columns
    columns = df1.columns
    
    # force columns to RangeIndex
    df1.columns = np.arange(df1.shape[1])
    
    # rename the columns with the new name
    df1.columns = pd.MultiIndex.from_tuples([
        (x,a,y) for (x,y),a in zip(columns, [1,2,1,3])
    ])
    

    Output:

      first     second    
          1   2      1   3
          D   a      b   c
    0     A   1      2   3
    1     B  -4      5  -6
    2     d   7      0   9
    

    Intestingly, a direct reassignment of the columns throw a warning and did not do anything, hence the go-around.