How to Sort Pandas DataFrame?
Last Updated :
10 Jul, 2025
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 DataFrameIn 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:
Sorting in descending orderParameters 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:
Sorting by Multiple ColumnsThis 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:
Sorting by Missing ValuesThis 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:
Sorting by IndexWe 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:
Sort by index in descending order5. 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:

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:
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:
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:
Sorting with Key FunctionsThis 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
How to Sort a Pandas DataFrame by Date? In the real world, we can come across datasets of any form that may include the date inside them too. These datasets can be present in any file format like .CSV, .xlsx, .txt, etc. To load this data inside Python, we use a library named Pandas which provides us a plethora of functions and methods to
3 min read
Pandas dataframe.sort_index() Pandas is one of those packages and makes importing and analyzing data much easier. When working with DataFrames, Pandas is used for handling tabular data. Let's learn Pandas DataFrame sort_index() method, which is used to sort the DataFrame based on index or column labels.Pandas sort_index() functi
3 min read
Pandas Dataframe.sort_values() In Pandas, sort_values() function sorts a DataFrame by one or more columns in ascending or descending order. This method is essential for organizing and analyzing large datasets effectively.Syntax: DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
2 min read
Sorting rows in pandas DataFrame Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). We often need to do certain operations on both rows and column while handling the data. Letâs see how to sort rows in pandas DataFrame. Code #1: Sorting rows by Sc
2 min read
Ranking Rows of Pandas DataFrame To rank the rows of Pandas DataFrame we can use the DataFrame.rank() method which returns a rank of every respective index of a series passed. The rank is returned on the basis of position after sorting. Example #1 : Here we will create a DataFrame of movies and rank them based on their ratings. Pyt
2 min read
Pandas DataFrame A Pandas DataFrame is a two-dimensional table-like structure in Python where data is arranged in rows and columns. Itâs one of the most commonly used tools for handling data and makes it easy to organize, analyze and manipulate data. It can store different types of data such as numbers, text and dat
10 min read