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 –
import pandas as pd
data = pd.read_csv( "nba.csv" )
data.sort_values([ "Team" , "Name" ], axis = 0 ,
ascending = True , inplace = True )
data
|
Output:

After Sorting –
import pandas as pd
data = pd.read_csv( "nba.csv" )
data.sort_values([ "Team" , "Name" ], axis = 0 ,
ascending = True , inplace = True )
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.
import pandas as pd
data = pd.read_csv( "nba.csv" )
data.sort_values([ "Team" , "Name" ], axis = 0 ,
ascending = [ True , False ], inplace = True )
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.
import pandas as pd
data = pd.read_csv( "nba.csv" )
data.sort_values([ "Team" , "Age" , "Height" ], axis = 0 ,
ascending = [ False , True , False ],
inplace = True )
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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Sep, 2018
Like Article
Save Article