greencode
greencode

Reputation: 57

pandas sort more than one column not working

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:

enter image description here

Upvotes: 0

Views: 78

Answers (2)

Tomy Tjandra
Tomy Tjandra

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

BENY
BENY

Reputation: 323226

You should assign it back and switch

df = df.sort_values(by=['name', 'values'], ascending=[True, False])

Upvotes: 0

Related Questions