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.
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
You just have to call df.sort_index after setting :
df1=pd.DataFrame(columns=pd.MultiIndex.from_product((('a','b'),
(1,2))),data=np.arange(4).reshape(1,-1))
df2=pd.DataFrame(columns=pd.MultiIndex.from_tuples((('a','3'),('b',5))),data=[[9,9]])
df=df1.join(df2)
#    a     b     a  b
#    1  2  1  2  3  5
# 0  0  1  2  3  9  9
df.sort_index(axis=1,inplace=True)
#    a        b      
#    1  2  3  1  2  5
# 0  0  1  9  2  3  9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With