0

I have a Dataframe with columns named alphanumerically as 'A#1 , A#0.25, A#10, A#2, B#1, B#0.25, B#10, B#2'

I want to arrange them alphanumerically like 'A#0.25, A#1, A#10, A#2, B#0.25, B#1, B#2, B#10'- so first on alphabetical part before # and then on numeric part after #.

Is there a way to do this directly ?

dayum
  • 1,073
  • 15
  • 31
  • Check this out! https://stackoverflow.com/questions/11067027/re-ordering-columns-in-pandas-dataframe-based-on-column-name – bumblebee Feb 18 '19 at 05:17

1 Answers1

1

Using natsort

import natsort
df=df.reindex(columns=natsort.natsorted(df.columns))
df
Out[115]: 
   A#0.25  A#1  A#2  A#10  B#0.25  B#1  B#2  B#10
0       0    0    0     0       0    0    0     0

Data input

df
Out[112]: 
   A#1  A#0.25  A#10  A#2  B#1  B#0.25  B#10  B#2
0    0       0     0    0    0       0     0    0
BENY
  • 317,841
  • 20
  • 164
  • 234
  • thanks @Wen-Ben. I wonder if there is a way to do this without using another library and staying within numpy/pandas – dayum Feb 19 '19 at 06:21