I want to sort the second column names of multiindex
but the columns must keep their corresponding column values.
So I tried this code:
df.columns=df.sort_index(axis=1,level=[1],ascending=[True]).columns
But is sorting only the names of the columns, and the columns are changing the data.
How can I sort the name of the columns without changing their corresponding data?
Thanks in advance
Sample DataFrame:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.arange(1, 17).reshape(-1, 4),
columns=pd.MultiIndex.from_product([['a', 'b'], ['d', 'c']]))
a b
d c d c
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
3 13 14 15 16
Modifying columns
will overwrite the columns and not affect the values:
df.columns = ['w', 'x', 'y', 'z']
w x y z
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
3 13 14 15 16
*Notice no values have changed even though the columns have been.
To actually sort the DataFrame based on values, overwrite the DataFrame with the return from sort_index
:
df = df.sort_index(axis=1, level=1, ascending=True)
Or inplace
:
df.sort_index(axis=1, level=1, ascending=True, inplace=True)
df
:
a b a b
c c d d
0 2 4 1 3
1 6 8 5 7
2 10 12 9 11
3 14 16 13 15
It's interesting that in my search for duplicates the first and closest result I could find is Sorting columns of multiindex dataframe which has an answer very similar to OP's attempt. But in that question the goal was to "sort only the column names and keep the values as it is within each column" which does not sort the DataFrame values but only modifies the DataFrame cosmetically.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.