Home > Mobile >  sorting pandas dataframe by column alphabetically after first 4 characters
sorting pandas dataframe by column alphabetically after first 4 characters

Time:10-15

I have a dataframe like this

    new_col    new_elements       new_val     old_col   old_elements   old_val
 0  0          384444683          593         2         423483819      480
 1  1          384444684          594         32        248239340      341
 2  2          384444686          596         0         249289049      342

and I want this:

    new_col    old_col   new_elements      old_elements     new_val     old_val   
 0  0          2         384444683         423483819        593         480     
 1  1          32        384444684         248239340        594         341
 2  2          0         384444686         249289049        596         342

I know that df.sort_index(axis=1) will alphabetically sort my columns, but they already are sorted that way now. What I want is for them to be sorted alphabetically after the prefix (first 4 characters)

CodePudding user response:

col = df.columns
col = sorted(col,key=lambda x: x[4:])
col
df = df[col]
df

enter image description here

Putting altogether df = df[sorted(df.columns,key=lambda x: x[4:])]

CodePudding user response:

I'm no expert but this is how I'd go about it:

fields = ['new_col', 'old_col', 'new_elements', 'old_elements', 'new_val', 'old_val']

df = df[fields]
  • Related