
- 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 – Descending Order Sort grouped Pandas dataframe by group size?
To group Pandas dataframe, we use groupby(). To sort grouped dataframe in descending order, use sort_values(). The size() method is used to get the dataframe size.
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)
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"\nSorted in Descending order..."; print(dataFrame.groupby('Reg_Price').size().sort_values(ascending=False))
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
- Related Articles
- Python – Ascending Order Sort grouped Pandas dataframe by group size?
- Python – Sort grouped Pandas dataframe by group size?
- Python Pandas - Sort DataFrame in descending order according to the element frequency
- Write a Python program to sort a given DataFrame by name column in descending order
- 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 Pandas - How to Sort MultiIndex at a specific level in descending order
- Python Pandas - Sort DataFrame in ascending order according to the element frequency
- Sort by date & time in descending order in MySQL?
- Sort MongoDB documents in descending order
- Sort index in ascending order – Python Pandas
- Python – Sort Dictionaries by Size
- Python - Filter Pandas DataFrame by Time

Advertisements