1

My present data frame columns are ordered as

df.columns = ['S1FF', 'S1Ipmax', 'S2POA', 
       ...
       'M04Vpmax', 'M04elec_event', 'M04group', 'M04met_event', 'M04mode',
       'M04validation_1min', 'M04validation_2min', 'M04S1T', 'M04S2T',
       'M04datetime']  # Columns start with S1,S2, M01 to M24

I want to sort them in reverse alphabetical order. My present code is

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

My present output is

df.columns = ['M01FF', 'M01Ipmax', 'M02POA', 
       ...
       'S1FF', 'S1Ipmax', 'S2POA']

But my expected output is reverse alphabetical order as

df.columns = ['S1FF', 'S1Ipmax', 'S2POA','M01FF', 'M01Ipmax', 'M02POA', 
       ...
       'M23FF', 'M24Ipmax', 'M24POA']
Mainland
  • 4,110
  • 3
  • 25
  • 56

1 Answers1

2

Use key parameter by first value of string, here S and M with reverse=True:

L = ['M01FF', 'M01Ipmax', 'M02POA', 'S1FF', 'S1Ipmax', 'S2POA']
df = pd.DataFrame(columns=L)
print (df)
Empty DataFrame
Columns: [M01FF, M01Ipmax, M02POA, S1FF, S1Ipmax, S2POA]
Index: []

df = df[sorted(df.columns, key=lambda x: x[0], reverse = True)]
print (df)
Empty DataFrame
Columns: [S1FF, S1Ipmax, S2POA, M01FF, M01Ipmax, M02POA]
Index: []

EDIT:

import natsort as ns
s = ns.natsorted([x for x in df.columns if x[0] == 'S'])
m = ns.natsorted([x for x in df.columns if x[0] == 'M'])
df = df[s + m]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks for the solution. Yes! S1, S2 did you appear first but it could not sort the M columns. I stil see M03,M04 columns at very last and M23, M24 columns in between. How to sort their order? – Mainland Jan 22 '20 at 11:44
  • @Mainland - I test `L = ['M01FF', 'M01Ipmax', 'M02POA', 'S1FF', 'S1Ipmax', 'S2POA','M23','M24']` and working for me, there is no some trailing whitespace like `' M03'` ? – jezrael Jan 22 '20 at 11:47
  • 1
    Agree with you but i still see following order after executing above code : `Columns: [S1FF, S1Ipmax, S1POA, S1Isc, S1Pmax, S1Voc, S1Vpmax, S2FF, S2Ipmax, S2POA,M01FF, M01Ipmax, M01POA, M02FF, M02Ipmax, M02POA, M02datetime, M10FF, M10Ipmax, M10POA, M10Isc, M10Pmax, M10validation_1min, M10validation_2min, M10S1T, M10S2T, M10datetime, M09FF, M09Ipmax, M09POA, M09Isc, M09Pmax, M09Voc, M09Vpmax, M09elec_event, M09group, M09met_event, M09mode, M09validation_1min, M09validation_2min, M09S1T, M09S2T, M09datetime, M05FF, M05Ipmax, M05POA, M05Isc, ...]` – Mainland Jan 22 '20 at 11:51
  • @Mainland - Can you check now? – jezrael Jan 22 '20 at 11:58
  • `df[sorted(df.columns, key=lambda x: x[0], reverse = True)]` this one? its old line right? No change in response. – Mainland Jan 22 '20 at 12:00
  • Yes! now I see a proper order. Even in that I see following `['S1FF', 'S1Ipmax', 'S1POA', 'S1Isc', 'S1Pmax', 'S1Voc', 'S1Vpmax', 'S1elec_event', 'S1group', 'S1met_event', 'S1mode', 'S1validation_1min', 'S1validation_2min', 'S1S1T', 'S1S2T', 'S1datetime', 'S2FF', 'S2Ipmax', 'S2POA', 'S2Isc', 'S2Pmax', 'S2Voc', 'S2Vpmax', 'S2elec_event', 'S2group', 'S2met_event', 'S2mode', 'S2validation_1min', 'S2validation_2min', 'S2S1T', 'S2S2T', 'S2datetime',` S2datetime is coming last. But it should come before S2group right. What do you think? – Mainland Jan 22 '20 at 12:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206444/discussion-between-jezrael-and-mainland). – jezrael Jan 22 '20 at 12:10