
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python Pandas - Sort DataFrame in ascending order according to the element frequency
To sort data in ascending or descending order, use sort_values() method. For ascending order, use the following is the sort_values() method −
ascending=True
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 ascending order according to the element frequency, we need to count the occurrences. Therefore, count() is also used with sort_values() set for asscending order sort −
dataFrame.groupby(['Car'])['Reg_Price'].count().reset_index(name='Count').sort_values(['Count'], ascending=True)
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 ascending order according to the element frequency dataFrame = dataFrame.groupby(['Car'])['Reg_Price'].count().reset_index(name='Count').sort_values(['Count'], ascending=True) print"\nSorting DataFrame in ascending order ...\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 in ascending order ... Car Count 2 Mercedes 1 3 Mustang 1 0 BMW 2 1 Lexus 2
- Related Articles
- Python Pandas - Sort DataFrame in descending order according to the element frequency
- Python – Ascending Order Sort grouped Pandas dataframe by group size?
- Sort index in ascending order – Python Pandas
- Python – Descending Order Sort grouped Pandas dataframe by group size?
- Python program to sort out words of the sentence in ascending order
- Python program to sort the elements of an array in ascending order
- Frequency plot in Python/Pandas DataFrame using Matplotlib
- How to perform ascending order sort in MongoDB?
- Program to merge intervals and sort them in ascending order in Python
- Python - Reverse the column order of the Pandas DataFrame
- Program to sort a given linked list into ascending order in python
- Python program to sort a list according to the second element in sublist
- Python program to sort a list according to the second element in the sublist.
- How to sort Java array elements in ascending order?
- 8085 Program to perform selection sort in ascending order

Advertisements