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 –

filter_none

edit
close

play_arrow

link
brightness_4
code

#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

chevron_right


Output:

 
After Sorting –

filter_none

edit
close

play_arrow

link
brightness_4
code

#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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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

chevron_right


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.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.