Reputation: 57
I am looking to sort a numerical column (desc) and a text column (asc) but it is not working as expected in pandas.
Here is my code:
df.sort_values(by=['values', 'name'], ascending=[False, True])
The values are showing desc but the names are not in ascending order.
This is the desired result I am looking for:
Upvotes: 0
Views: 78
Reputation: 71
Just change the order on how we sort the values:
df.sort_values(by=['name', 'values'], ascending=[True, False])
We have to sort it by name
first, and then values
. Not the other way.
Upvotes: 1
Reputation: 323226
You should assign it back and switch
df = df.sort_values(by=['name', 'values'], ascending=[True, False])
Upvotes: 0