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.
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.
Dataframe.sort_values() Multiple Parameter Sorting:
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 –
After Sorting –
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.
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.
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.map()
- Python | Pandas series.str.get()
- Python | Pandas Index.where
- Python | Pandas DataFrame.loc
- Python | Pandas Series.xs
- Python | Pandas DatetimeIndex.day
- Python | Pandas Panel.sum()
- Python | Pandas Series.last()
- Python | Pandas Series.str.pad()
- Python | Pandas TimedeltaIndex.contains
- Python | Pandas Series.ix
- Python | Pandas Series.loc
- Python | Pandas Index.contains()
- Python | Pandas DataFrame.where()
- Python | Pandas dataframe.eq()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.