0

I need to sort(in a descending order) a dataframe according to multiple columns using the following piece of code:

df = df.sort_values(measures, ascending = (False,False,False,False,False))

where 'measures' is a list of columns names. As a result I only get the dataframe sorted by the first column (ms1) in 'measures' list.

a snippet of the dataframe before sorting:

     Features      ms1    ...            ms4           ms5
0        age       0.188  ...            0.025         0.994
1        sex       0.556  ...            0.040         8.080
2         cp       0.437  ...            0.054         20.121
3   trestbps       0.199  ...            0.015         0.512
4       chol       0.194  ...            0.002         0.085
5        fbs       0.622  ...            0.030         0.055

And after sorting:

     Features      ms1    ...            ms4           ms5
5        fbs       0.622  ...            0.030         0.055
8      exang       0.593  ...            0.023         36.842
1        sex       0.556  ...            0.040         8.080
12      thal       0.527  ...            0.007         1.902
6    restecg       0.488  ...            0.067         1.219

does anyone know why it is not working properly

2
  • What you are passing to the ascending parameter is a tuple when a list is required. Might be worth it to read up on the differences which should help understand why you will generally see that parameters will require lists vs tuples. Here is a good post on the topic: stackoverflow.com/questions/1708510/… Commented May 28, 2021 at 10:10
  • @Timbolt I actually did use a list instead of a tuple but had the same output! Commented May 28, 2021 at 10:18

2 Answers 2

1

'ascending' parameter takes a bool or list of bool as per documentation. So, try this instead -

df.sort_values(measures, ascending = [False,False,False,False,False])
Sign up to request clarification or add additional context in comments.

Comments

1

When you sorted by the first column(ms1) the another columns is following the ms1.But if there are same value in ms1 then the same values rows will sorted values by the second column.And so on...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.