In this article we will discuss how to sort rows in ascending and descending order based on values in a single or multiple columns . Also, how to sort columns based on values in rows using DataFrame.sort_values()

DataFrame.sort_values()

In Python’s Pandas library, Dataframe class provides a member function to sort the content of dataframe i.e.

Arguments :

  • by : A string or list of strings basically either column names or index labels based on which sorting will be done.
  • axis : If axis is 0, then name or list of names in by argument will be considered as column names. Default is 0
    • If axis is 1, then name or list of names in by argument will be considered as row index labels
  • ascending : If True sort in ascending else sort in descending order. Default is True
  • inplace : If True, perform operation in-place in Dataframe
  • na_position : Decides the position of NaNs after sorting i.e. irst puts NaNs at the beginning, last puts NaNs at the end
    Default value is ‘first’

It returns a sorted dataframe object. Also, if inplace argument is not True then it will return a sorted copy of given dataframe, instead of modifying the original Dataframe. Whereas, if inplace argument is True then it will make the current dataframe sorted.

Let’s understand by some examples,

First of all create a Dataframe object i.e.

Contents of the created dataframe object dfObj are,

Now let’s see how to sort this dataframe in different ways,

Sort Dataframe rows based on a single column

To sort all the rows in above datafarme based on a column ‘Name’, we are going to pass the column name in by argument i.e.

Output:

As we can see in the output, it sorted all the rows in dataframe based on column ‘Name’. Now if you will observe in deep you will found the for 4 rows column name has same value. Now what if we want to sort in such a ways values are same one column then use 2nd column for sorting those rows ?
Let’s see how to do that ..

Sort Dataframe rows based on a multiple columns

To sort all the rows in above datafarme based on two column i.e. ‘Name’ & ‘Marks’, we are going to pass the column names as list in by argument i.e.

Output:

Now it will first sort all the rows based on column ‘Name’. If some rows has same value in ‘Name’ column then it will sort those rows based on value in ‘Marks’ column.

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.

Output:

It will sort all the rows in Dataframe based on column ‘Name’ in descending orders.

Sort Dataframe rows based on a column in Place

To sort all the rows in above datafarme based on a single columns in place pass an extra argument inplace with value True along with other arguments i.e.

Output:

It will sort all the rows in Dataframe based on column ‘Name’ in place.

Till now we sorted the dataframe rows based on columns what if we want to vice versa i.e.

Sort columns of a Dataframe based on a single or multiple rows

Suppose we a dataframe of numbers i.e.

Contents of the dataframe are,

Sort columns of a Dataframe based on a single row

To sort columns of this dataframe based on a single row pass the row index labels in by argument and axis=1 i.e.

Output:

So, all the columns in dataframe are sorted based on a single row with index label ‘b’.

Sort columns of a Dataframe in Descending Order based on a single row

To sort columns of this dataframe in descending order based on a single row pass argument ascending=False along with other arguments i.e.

Output:

Sort columns of a Dataframe based on a multiple rows

To sort the columns in dataframe are sorted based on multiple rows with index labels ‘b’ & ‘c’ pass the list in by argument and axis=1 i.e.

Output:

Complete example is as follows,

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.