77

I'm trying to sort a dataframe by descending. I put 'False' in the ascending argument, but my order is still ascending.

My code is:

from pandas import DataFrame
import pandas as pd

d = {'one':[2,3,1,4,5],
     'two':[5,4,3,2,1],
     'letter':['a','a','b','b','c']}

df = DataFrame(d)

test = df.sort(['one'], ascending=[False])

but the output is

  letter  one  two
2      b    1    3
0      a    2    5
1      a    3    4
3      b    4    2
4      c    5    1
2
  • 2
    Your code actually gives the desired results on pandas version 0.14.1, so you may want to upgrade if possible. Commented Jul 28, 2014 at 6:12
  • 1
    Since this question refers to syntax which has worked fine since 0.14.1, (and was a fairly obvious typo before that), probably should close as not-an-issue. Commented May 27, 2018 at 11:59

6 Answers 6

104

New syntax (either):

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

1 Comment

You can do this for multiple columns too: test = df.sort_values(['one','two'], ascending=[0,0])
68

Edit: This is out of date, see @Merlin's answer.

[False], being a nonempty list, is not the same as False. You should write:

test = df.sort('one', ascending=False)

1 Comment

Just to gently point to the answers below to work with latest Pandas version.
25

For pandas 0.17 and above, use this :

test = df.sort_values('one', ascending=False)

Since 'one' is a series in the pandas data frame, hence pandas will not accept the arguments in the form of a list.

2 Comments

I seriously doubt that "hence pandas will not accept the arguments in the form of a list." is true. It does accept ascending=[False] on pandas 0.19. Could you elaborate?
BTW, since typing "ascending=False" is a lot to type and I use it a lot, sort_values('one')[::-1] will work if you take the performance hit.
4
from pandas import DataFrame
import pandas as pd

d = {'one':[2,3,1,4,5],
 'two':[5,4,3,2,1],
 'letter':['a','a','b','b','c']}

df = DataFrame(d)

test = df.sort_values(['one'], ascending=False)
test

Comments

0
from pandas import DataFrame
import pandas as pd

d = {'one':[2,3,1,4,5], 'two':[5,4,3,2,1], 'letter':['a','a','b','b','c']}
df = DataFrame(d)

#check below statement
df.groupby(["one"]).count().sort_values("one", ascending=False)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html

I don't think you should ever provide the False value in square brackets (ever), also the column values when they are more than one, then only they are provided as a list! Not like ['one'].

test = df.sort_values(by='one', ascending = False)

1 Comment

Use you square brackets, because you might need to sort multiple columns. Look at OP question.

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.