Skip to content Skip to sidebar Skip to footer

Pandas Sort Columns By Name

I have the following dataframe, where I would like to sort the columns according to the name. 1 | 13_1 | 13_10| 13_2 | 2 | 3 9 | 31 | 2 | 1 | 3 | 4 I am trying to s

Solution 1:

natsort

from natsort import natsorted

df = df.reindex(natsorted(df.columns), axis=1)

#   1  2  3  13_1  13_2  13_10#0  9  3  4    31     1      2

Solution 2:

first of all, natsort from the other answers looks awesome, I'd totally use that.

In case you don't want to install a new package:

Seems like you want to sort, numerically, first by the number before the _ and then by the number after it as a tie break. this means you just want a tuple sort order, when splitting to tuple by _.

try this:

df = df[sorted(df.columns, key=lambda x: tuple(map(int,x.split('_'))))]

Output:

1  2  3  13_1  13_2  13_10
9  3  4    31     1      2

Solution 3:

Here is one way using natsorted

from natsort import natsorted, ns
df=df.reindex(columns=natsorted(df.columns))
Out[337]: 
   12313_113_213_1009343112

Another way we stack with pandas no 3rd party lib :-)

idx=df.columns.to_series().str.split('_',expand=True).astype(float).reset_index(drop=True).sort_values([0,1]).index
df=df.iloc[:,idx]
Out[355]: 
   12313_1  13_2  13_10
09343112

Post a Comment for "Pandas Sort Columns By Name"