name | qty | id |
---|---|---|
orange4 | 1 | 10pqw102 |
orange3 | 2 | 10pxa920 |
apple2 | 3 | 20asa399 |
apple1 | 4 | 20asw987 |
Expected output
name | qty | id |
---|---|---|
orange4 | 1 | 10pqw102 |
apple2 | 3 | 20asa399 |
orange3 | 2 | 10pxa920 |
apple1 | 4 | 20asw987 |
name | qty | id |
---|---|---|
orange4 | 1 | 10pqw102 |
orange3 | 2 | 10pxa920 |
apple2 | 3 | 20asa399 |
apple1 | 4 | 20asw987 |
name | qty | id |
---|---|---|
orange4 | 1 | 10pqw102 |
apple2 | 3 | 20asa399 |
orange3 | 2 | 10pxa920 |
apple1 | 4 | 20asw987 |
Use key
parameter in DataFrame.sort_values
:
df = df.sort_values(by='id', key=lambda col: col.str[-3:].astype(int))
For oldier pandas versions:
df = df.iloc[df['id'].str[-3:].astype(int).argsort()]
df.sort_values(df['ColC'].str.extract('(\d{3}$)').astype(int))
but i think OP wants to be sorted by ID
column not ColC