Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, sort descending dataframe with pandas

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 
like image 266
user3636476 Avatar asked Jul 28 '14 05:07

user3636476


People also ask

How do you sort descending values in Python?

Python List sort() - Sorts Ascending or Descending List. The list. sort() method sorts the elements of a list in ascending or descending order using the default < comparisons operator between items. Use the key parameter to pass the function name to be used for comparison instead of the default < operator.

How do you sort rows in descending order in Python?

Sort Dataframe rows based on columns in Descending Order To sort all the rows in above datafarme based on columns in descending order pass argument ascending with value False along with by arguments i.e. It will sort all the rows in Dataframe based on column 'Name' in descending orders.

How do you sort pandas DataFrame ascending and descending?

You can sort pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True.

How do you sort a DataFrame in ascending order Python?

Sorting Your DataFrame on a Single Column. To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.


Video Answer


1 Answers

New syntax (either):

 test = df.sort_values(['one'], ascending=[False])  test = df.sort_values(['one'], ascending=[0]) 
like image 78
Merlin Avatar answered Sep 27 '22 20:09

Merlin