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

17 Sep, 2018
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 TeamIn 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 packageimport pandas as pd  #making data frame from csv filedata=pd.read_csv("nba.csv")  #sorting data frame by Team and then By namesdata.sort_values(["Team", "Name"], axis=0,                 ascending=True, inplace=True)  #displaydata
Output: After Sorting –
#importing pandas packageimport pandas as pd  #making data frame from csv filedata=pd.read_csv("nba.csv")  #sorting data frame by Team and then By namesdata.sort_values(["Team", "Name"], axis=0,                 ascending=True, inplace=True)  #displaydata
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 packageimport pandas as pd  #making data frame from csv filedata=pd.read_csv("nba.csv")  #sorting data frame by Team and then By namesdata.sort_values(["Team", "Name"], axis=0,                 ascending=[True,False], inplace=True)  #displaydata
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 packageimport pandas as pd  #making data frame from csv filedata=pd.read_csv("nba.csv")  #sorting data frame by Team, age and heightdata.sort_values(["Team", "Age", "Height"], axis=0,                 ascending=[False,True,False],inplace=True)  #displaydata
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.
Python pandas-dataFrame
Python pandas-dataFrame-methods
python-modules
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Reading and Writing to text files in Python
sum() function in Python
Create a Pandas DataFrame from Lists
How to drop one or multiple columns in Pandas Dataframe
*args and **kwargs in Python
Graph Plotting in Python | Set 1