Search code examples
pythonpandasdataframemulti-index

Regrouping pandas multiindex columns


I am running into an issue where adding new columns to a multiindex column DataFrame causes the new columns to append to the end of the DataFrame. E.g.:

Group 1         | Group 2        | Group 1 | Group 2 |
------------------------------------------------------
Sub 1  | Sub 2  | Sub 1  | Sub 2 | New Sub | New Sub |

Whereas what I want is:

Group 1                   | Group 2                  |
------------------------------------------------------
Sub 1  | Sub 2  | New Sub | Sub 1  | Sub 2 | New Sub |

Is there a way to re-group/order my multiindex to do this? Note- I do not want to re-order the Sub Groups by name, as New Sub needs to go at the end, and alphabetically might not sort correctly.


Solution

  • I think you need reindex or reindex_axis by custom list:

    df1=pd.DataFrame(columns=pd.MultiIndex.from_product((('C','R', 'A'),(1,2))),
                     data=np.arange(6).reshape(1,-1))
    df2=pd.DataFrame(columns=pd.MultiIndex.from_tuples((('C','3'),('R',5),('A',4))),
                     data=[[9,9,4]])
    df=df1.join(df2)
    print (df)
      C     R     A     C  R  A
       1  2  1  2  1  2  3  5  4
    0  0  1  2  3  4  5  9  9  4
    
    df1 = df.reindex(columns = ['C','R','A'], level=0)
    print (df1)
       C        R        A      
       1  2  3  1  2  5  1  2  4
    0  0  1  9  2  3  9  4  5  4
    
    df1 = df.reindex_axis(['C','R','A'], level=0, axis=1)
    print (df1)
       C        R        A      
       1  2  3  1  2  5  1  2  4
    0  0  1  9  2  3  9  4  5  4