Reputation: 1093
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 ?
Upvotes: 0
Views: 34
Reputation: 323306
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
Upvotes: 1