
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Grouped Pandas DataFrame by Group Size in Python
To group Pandas data frame, we use groupby(). To sort grouped data frames in ascending or descending order, use sort_values(). The size() method is used to get the data frame size.
Steps Involved
The steps included in sorting the panda's data frame by its group size are as follows.
-
Importing the panda's library and Creating a Pandas dataframe.
-
Grouping the columns by using the groupby() function and sorting the values by using sort_values() in descending order.
-
Sorting the values in ascending order by using the sort_values() function.
Creating a pandas dataframe
First import the Pandas library, and create a Pandas data frame.
import pandas as pd # dataframe with one of the columns as Reg_Price dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900], })
Grouping and Sorting
To group according to the Reg_Price column and sort in descending order by setting ascending=False
dataFrame.groupby('Reg_Price').size().sort_values(ascending=False)
Next, group according to the Reg_Price column and sort in ascending order by setting ascending=True
dataFrame.groupby('Reg_Price').size().sort_values(ascending=True)
Example
In the example code the DataFrame contains columns Car and Reg_Price. We group by the Reg_Price column using the groupby() function, and calculate the size of each group, then sort these group sizes in descending and ascending order using the sort_values() function.
import pandas as pd # dataframe with one of the columns as Reg_Price dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] } ) print("DataFrame...\n",dataFrame) # group according to Reg_Price column and sort in descending order print("Sorted in Descending order...\n") print(dataFrame.groupby('Reg_Price').size().sort_values(ascending=False)) # group according to Reg_Price column and sort in ascending order print("Sorted in Ascending order...\n") print(dataFrame.groupby('Reg_Price').size().sort_values(ascending=True))
Input DataFrame
Car | Reg_Price | |
---|---|---|
0 | BMW | 1000 |
1 | Lexus | 1400 |
2 | Audi | 1000 |
3 | Mercedes | 900 |
4 | Jaguar | 1700 |
5 | Bentley | 900 |
Sorted in Descending order
Reg_price | |
---|---|
1000 | 2 |
900 | 2 |
1700 | 1 |
1400 | 1 |
Sorted in Ascending order
Reg_price | |
---|---|
1400 | 1 |
1700 | 1 |
900 | 2 |
1000 | 2 |