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 ColumnThe 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 order Parameters of sort_values():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 ValuesIn 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 IndexIn 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 order 5. Choosing a Sorting AlgorithmPandas 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:
MergeSort3. 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:
HeapSort6. Applying Custom Sorting LogicNote: HeapSort being unstable, may not preserve this order and in some cases like the one above, it swaps rows with the same sorting key.
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4