Open In App

How to Sort Pandas DataFrame?

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We can perform sorting in Pandas Dataframe. This article will discuss how to sort Pandas DataFrame using various methods in Python.

Sorting Data Frames in Pandas

Creating a Pandas Dataframe for Demonstration, Here, we have created a dataframe in which we will perform various sorting functions.

Python3




# importing pandas library
import pandas as pd
 
# creating and initializing a nested list
age_list = [['Afghanistan', 1952, 8425333, 'Asia'],
            ['Australia', 1957, 9712569, 'Oceania'],
            ['Brazil', 1962, 76039390, 'Americas'],
            ['China', 1957, 637408000, 'Asia'],
            ['France', 1957, 44310863, 'Europe'],
            ['India', 1952, 3.72e+08, 'Asia'],
            ['United States', 1957, 171984000, 'Americas']]
 
# creating a pandas dataframe
df = pd.DataFrame(age_list, columns=['Country', 'Year',
                                     'Population', 'Continent'])
 
df


Output

Sort Pandas DataFrame

Sort Pandas DataFrame

Sorting Pandas Data Frame

In order to sort the data frame in pandas, the function sort_values() is used. Pandas sort_values() can sort the data frame in Ascending or Descending order.

Pandas DataFrame Sorting in Ascending Order 

The code snippet sorts the DataFrame df in ascending order based on the ‘Country’ column. However, it does not store or display the sorted data frame.

Python3




# Sorting by column 'Country'
df.sort_values(by=['Country'])


Output:

Sort Pandas DataFrame

Sort Pandas DataFrame

Sorting the Pandas DataFrame in Descending order 

The DataFrame df will be sorted in descending order based on the “Population” column, with the country having the highest population appearing at the top of the DataFrame.

Python3




# Sorting by column "Population"
df.sort_values(by=['Population'], ascending=False)


Output:

Sort Pandas DataFrame

Sort Pandas DataFrame

Sort Pandas DataFrame Based on Sampling

Here, we are sorting a DataFrame (df) based on the ‘Population’ column, arranging rows with missing values in ‘Population’ to appear first. The sort_values() method with the na_position='first' argument achieves this, prioritizing rows with missing values at the beginning of the sorted DataFrame.

Python3




# Sorting by column "Population"
# by putting missing values first
df.sort_values(by=['Population'], na_position='first')


Output:

Sort Pandas DataFrame

Sort Pandas DataFrame

Sorting Data frames by Multiple Columns

In this example, we are sorting a DataFrame (df) primarily by the ‘Country’ column in ascending order and, within each country group, by the ‘Continent’ column. The resulting DataFrame is sorted based on the specified column order, creating a sorted dataset.

Python3




# Sorting by columns "Country" and then "Continent"
df.sort_values(by=['Country', 'Continent'])


Output:

Sort Pandas DataFrame

Sort Pandas DataFrame

Sorting Data frames by Columns But in a Different Order

In this example, we are sorting a DataFrame (df) first by the ‘Country’ column in descending order and, within each country group, by the ‘Continent’ column in ascending order. The resulting DataFrame is organized based on the specified column sorting criteria.

Python3




# Sorting by columns "Country" in descending
# order and then "Continent" in ascending order
 
df.sort_values(by=['Country', 'Continent'],
               ascending=[False, True])


Output:

Sort Pandas DataFrame

Sort Pandas DataFrame



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads