python, sort descending dataframe with pandas

前端 未结 5 928
春和景丽
春和景丽 2020-12-01 17:58

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 im         


        
相关标签:
5条回答
  • 2020-12-01 18:10

    New syntax (either):

     test = df.sort_values(['one'], ascending=[False])
     test = df.sort_values(['one'], ascending=[0])
    
    0 讨论(0)
  • 2020-12-01 18:15

    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.

    0 讨论(0)
  • 2020-12-01 18:21

    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)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-01 18:35
    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
    
    0 讨论(0)
提交回复
热议问题