1

Suppose my dataframe df is as below

value_id = ['1_1', '1_2', '1_5', '10_1', '2_2', '3_2']
value_age = [28, 34, 54, 24, 12, 56]
df = pd.DataFrame({'id':value_id, 'age':value_age})

I want to sort this dataframe according to the first column (i.e. id). I want an output like this

id     age
1_1    28
1_2    34
1_5    54
2_2    12
3_2    56
10_1   24
Firenze
  • 365
  • 2
  • 11

1 Answers1

4

You can set id as index and use df.reindex, and use sorted with custom key parameter.

def f(x):
    v, v1 = map(int, x.split('_'))
    return v, v1

df.set_index('id').reindex(sorted(df.id,key=f))

      age
id
1_1    28
1_2    34
1_5    54
2_2    12
3_2    56
10_1   24
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • 3
    Nice solution, alternative with natsort: `import natsort as ns` , `df.set_index('id').loc[ns.natsorted(df['id'])]` – anky Jul 15 '20 at 12:30
  • 1
    i was thinking natsort too @anky but its not part of the standard lib yet :( – Umar.H Jul 15 '20 at 12:30
  • 1
    @anky I thought of using natural sort too but natural sort isn't included in stl, thanks for the suggestion, more about natsort here if anyone's looking for it https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort – Ch3steR Jul 15 '20 at 12:33
  • Nice solution. Just found out that instead of a custom key, you can also provide this key key= lambda x: (len (x), x)) – Firenze Jul 15 '20 at 12:35