- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Sort DataFrame in descending order according to the element frequency
To sort data in ascending or descending order, use sort_values() method. For descending order, use the following in the sort_values() method −
ascending=False
Import the required library −
import pandas as pd
Create a DataFrame with 3 columns −
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000],"Place": ['Pune', 'Delhi', 'Mumbai', 'Hyderabad', 'Bangalore', 'Chandigarh'] } )
To sort DataFrame in descending order according to the element frequency, we need to count the occurrences. Therefore, count() is also used with sort_values() set for descending order sort −
dataFrame.groupby(['Car'])['Reg_Price'].count().reset_index(name='Count').sort_values(['Count'], ascending=False)
Example
Following is the code −
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000],"Place": ['Pune', 'Delhi', 'Mumbai', 'Hyderabad', 'Bangalore', 'Chandigarh'] } ) print"DataFrame ...\n",dataFrame # Sort DataFrame in descending order according to the element frequency dataFrame = dataFrame.groupby(['Car'])['Reg_Price'].count().reset_index(name='Count').sort_values(['Count'], ascending=False) print"\nSorting DataFrame ...\n",dataFrame
Output
This will produce the following output −
DataFrame ... Car Place Reg_Price 0 BMW Pune 7000 1 Lexus Delhi 1500 2 BMW Mumbai 5000 3 Mustang Hyderabad 8000 4 Mercedes Bangalore 9000 5 Lexus Chandigarh 2000 Sorting DataFrame ... Car Count 0 BMW 2 1 Lexus 2 2 Mercedes 1 3 Mustang 1
- Related Articles
- Python Pandas - Sort DataFrame in ascending order according to the element frequency
- Python – Descending Order Sort grouped Pandas dataframe by group size?
- Python Pandas - How to Sort MultiIndex at a specific level in descending order
- Write a Python program to sort a given DataFrame by name column in descending order
- Python – Ascending Order Sort grouped Pandas dataframe by group size?
- Python program to sort the elements of an array in descending order
- Sort MongoDB documents in descending order
- Frequency plot in Python/Pandas DataFrame using Matplotlib
- How to perform descending order sort in MongoDB?
- How to sort TreeSet in descending order in Java?
- Sort index in ascending order – Python Pandas
- Python - Reverse the column order of the Pandas DataFrame
- Python program to sort a list according to the second element in sublist
- C# program to sort an array in descending order
- 8085 Program to perform bubble sort in descending order

Advertisements