Open In App

Python | Pandas Dataframe.sort_values() | Set-2

Prerequisite: Pandas DataFrame.sort_values() | Set-1

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier.

Pandas sort_values() function sorts a data frame in Ascending or Descending order of passed Column. It’s different than the sorted Python function since it cannot sort a data frame and particular column cannot be selected.

Let’s discuss Dataframe.sort_values() Multiple Parameter Sorting:

Syntax:

DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’)

For link to CSV file Used in Code, click here.

Example #1: Sorting by Name and Team
In the following example, A data frame is made from the csv file and the data frame is sorted in ascending order of Team and in every Team the Name is also sorted in Ascending order.

Before Sorting –




#importing pandas package
import pandas as pd
  
#making data frame from csv file
data=pd.read_csv("nba.csv")
  
#sorting data frame by Team and then By names
data.sort_values(["Team", "Name"], axis=0,
                 ascending=True, inplace=True)
  
#display
data

Output:


 
After Sorting –




#importing pandas package
import pandas as pd
  
#making data frame from csv file
data=pd.read_csv("nba.csv")
  
#sorting data frame by Team and then By names
data.sort_values(["Team", "Name"], axis=0,
                 ascending=True, inplace=True)
  
#display
data

As shown in image, The Team is sorted first in Ascending order and then the Names are sorted in ascending order for every Team name.


 
Example #2: Passing list to Ascending Parameter

As shown in the above example, a Data frame can be sorted with respect to multiple columns by passing a list to the ‘by’ Parameter. We can also pass a list to the ‘ascending’ Parameter to tell pandas which column to sort how.
The index of Boolean in ‘ascending’ parameter should be same as the index of column name in ‘by’ Parameter.




#importing pandas package
import pandas as pd
  
#making data frame from csv file
data=pd.read_csv("nba.csv")
  
#sorting data frame by Team and then By names
data.sort_values(["Team", "Name"], axis=0,
                 ascending=[True,False], inplace=True)
  
#display
data

As shown in the image below, the data frame is sorted in ascending of Team name and for every Team name, the Names are sorted in descending order.

 
Example #3: Sorting using 3 Columns

In the following example, the same Data Frame is sorted by Team name. For every Team, the Data frame is sorted by Age and for every same Age the Data frame is sorted by Height. This example will explain how multiparameter sorting works in Data Frame.




#importing pandas package
import pandas as pd
  
#making data frame from csv file
data=pd.read_csv("nba.csv")
  
#sorting data frame by Team, age and height
data.sort_values(["Team", "Age", "Height"], axis=0,
                 ascending=[False,True,False],
inplace=True)
  
#display
data

As shown in the Image, The team name is sorted first, then the Age and for every age, the height is sorted. In the Team “Washington Wizards” there are 3 Players with age 30. Those 3 people are sorted by their Height in ascending order.


Article Tags :