Open In App

How to Sort Pandas DataFrame?

Last Updated : 10 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Sorting data is an important step in data analysis as it helps to organize and structure the information for easier interpretation and decision-making. Whether we're working with small datasets or large ones, sorting allows us to arrange data in a meaningful way.

Pandas provides the sort_values() method which allows us to sort a DataFrame by one or more columns in either ascending or descending order.

1. Sorting a DataFrame by a Single Column

The sort_values() method in Pandas makes it easy to sort our DataFrame by a single column. By default, it sorts in ascending order but we can customize this.

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'Score': [85, 90, 95, 80]}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by='Age')
print(sorted_df)

Output:

Sort-Pandas-DataFrame
Sort Pandas DataFrame

In this example, the DataFrame is sorted by the Age column in ascending order but here it is already sorted. If we need the sorting to be in descending order simply pass ascending=False:

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Age': [25, 30, 35, 40],'Score': [85, 90, 95, 80]}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by='Age',ascending=False)
print(sorted_df)

Output:

sort1
Sorting in descending order

Parameters of sort_values():

  • by: Specifies the column to sort by.
  • ascending: A boolean (True for ascending, False for descending).
  • inplace: If True, the original DataFrame is modified otherwise a new sorted DataFrame is returned.
  • na_position: Controls where NaN values are placed. Use 'first' to put NaNs at the top or 'last' (default) to place them at the end.
  • ignore_index: If True, resets the index after sorting.

2. Sorting a DataFrame by Multiple Columns

When sorting by multiple columns, Pandas allows us to specify a list of column names. This is useful when we want to sort by one column like age and if there are ties, sort by another column like salary.

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'Score': [85, 90, 95, 80]}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by=['Age', 'Score'])
print(sorted_df)

Output:

sort2
Sorting by Multiple Columns

This will sort first by Age and if multiple rows have the same Age, it will then sort those rows by Salary.

3. Sorting DataFrame with Missing Values

In real-world datasets, missing values (NaNs) are common. By default sort_values() places NaN values at the end. If we need them at the top, we can use the na_position parameter.

Python
import pandas as pd
data_with_nan = {"Name": ["Alice", "Bob", "Charlie", "David"],"Age": [28, 22, None, 22]}
df_nan = pd.DataFrame(data_with_nan)

sorted_df = df_nan.sort_values(by="Age", na_position="first")
print(sorted_df)

Output:

sort3
Sorting by Missing Values

This will ensure that any rows with missing values in the Age column are placed at the top of the DataFrame.

4. Sorting by Index

In addition to sorting by column values, we may also want to sort a DataFrame based on its index. This can be done using the sort_index() method in Pandas. By default, sort_index() sorts the DataFrame based on the index in ascending order.

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'Score': [85, 90, 95, 80]}
df = pd.DataFrame(data)

df_sorted_by_index = df.sort_index()
print(df_sorted_by_index)

Output:

sort2
Sorting by Index

We can also sort by index in descending order by passing the ascending=False argument.

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'Score': [85, 90, 95, 80]}
df = pd.DataFrame(data)
df_sorted_by_index_desc = df.sort_index(ascending=False)
print(df_sorted_by_index_desc)

Output:

sort6
Sort by index in descending order

5. Choosing a Sorting Algorithm

Pandas provides different sorting algorithms that we can choose using the kind parameter. Available options are:

1. QuickSort (kind='quicksort'): It is a highly efficient, divide-and-conquer sorting algorithm. It selects a "pivot" element and partitions the dataset into two halves: one with elements smaller than the pivot and the other with elements greater than the pivot.

Python
import pandas as pd

data = {
    "Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
    "Age": [28, 22, 25, 22, 28],
    "Score": [85, 90, 95, 80, 88]
}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by='Age', kind='quicksort')
print(sorted_df)

Output:

Sort-Pandas-DataFrame

2. MergeSort (kind='mergesort'): Divides the dataset into smaller subarrays, sorts them and then merges them back together in sorted order.

Python
import pandas as pd

data = {
    "Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
    "Age": [28, 22, 25, 22, 28],
    "Score": [85, 90, 95, 80, 88]
}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by='Age', kind='mergesort')
print(sorted_df)

Output:

sortmerge
MergeSort

3. HeapSort (kind= 'heapsort'): It is another comparison-based sorting algorithm that builds a heap data structure to systematically extract the largest or smallest element and reorder the dataset.

Python
import pandas as pd

data = {
    "Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
    "Age": [28, 22, 25, 22, 28],
    "Score": [85, 90, 95, 80, 88]
}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by='Age', kind='heapsort')
print(sorted_df)

Output:

sortheap
HeapSort

Note: HeapSort being unstable, may not preserve this order and in some cases like the one above, it swaps rows with the same sorting key.

6. Applying Custom Sorting Logic

We can also apply custom sorting logic using the key parameter. This is useful when we need to sort strings in a specific way such as ignoring case sensitivity.

Python
import pandas as pd
data = {
    "Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
    "Age": [28, 22, 25, 22, 28],
    "Score": [85, 90, 95, 80, 88]
}
df = pd.DataFrame(data)

sorted_df = df.sort_values(by='Name', key=lambda col: col.str.lower())
print(sorted_df)

Output:

sort5
Sorting with Key Functions

This ensures that names are sorted alphabetically without considering case differences.

Mastering data sorting in Pandas allows us to efficiently organize our data. With these techniques, we can make our analysis smoother and more manageable.


Sorting DataFrame in Pandas

Similar Reads