
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Sort grouped Pandas dataframe by group size?
To group Pandas dataframe, we use groupby(). To sort grouped dataframe in ascending or descending order, use sort_values(). The size() method is used to get the dataframe size.
For ascending order sort, use the following in sort_values() −
ascending=True
For descending order sort, use the following in sort_values() −
ascending=False
At first, create a pandas dataframe −
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] } )
Next, group according to Reg_Price column and sort in descending order −
dataFrame.groupby('Reg_Price').size().sort_values(ascending=False)
Next, group according to Reg_Price column and sort in ascending order −
dataFrame.groupby('Reg_Price').size().sort_values(ascending=True)
Example
Following is the code −
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))
Output
This will produce the following output −
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 dtype: int64 Sorted in Ascending order... Reg_Price 1400 1 1700 1 900 2 1000 2 dtype: int64
- Related Articles
- Python – Descending Order Sort grouped Pandas dataframe by group size?
- Python – Ascending Order Sort grouped Pandas dataframe by group size?
- Python - How to Group Pandas DataFrame by Month?
- Python - How to Group Pandas DataFrame by Days?
- Python - How to Group Pandas DataFrame by Minutes?
- Python - How to Group Pandas DataFrame by Year?
- Python – Sort Dictionaries by Size
- Python - Filter Pandas DataFrame by Time
- Python - Compute last of group values in a Pandas DataFrame
- Python - How to group DataFrame rows into list in Pandas?
- Python - Compute first of group values in a Pandas DataFrame
- Python Pandas - Subset DataFrame by Column Name
- Group-by and Sum in Python Pandas
- How to select the largest of each group in Python Pandas DataFrame?
- Python Pandas - Sort DataFrame in descending order according to the element frequency

Advertisements