Python Pandas Sorting: Types and Examples
In Python, sorting is a common operation that is used to organize data in a specific order. Pandas, a Python library for data analysis, provides several methods for sorting DataFrames.
In this tutorial, we will discuss the different types of sorting that can be performed in Pandas, along with examples of how to use them.
Types of Sorting in Pandas
Sorting refers to the arrangement of data in a particular format or order. This could be in ascending or descending order, based on one or more columns. Python Pandas provides two kinds of sorting techniques:
- Label-based sorting using
sort_index()
: This method sorts a DataFrame by its index. The default sorting order is ascending, but you can also specify descending order by passing theascending=False
argument. - Value-based sorting using
sort_values()
: This method sorts a DataFrame by the values in a specified column. The default sorting order is ascending, but you can also specify descending order by passing theascending=False
argument.
sort_index()
The sort_index()
function is used to sort the data frame or series by its index in ascending or descending order.
Here's an example of how we can use sort_index()
.
import pandas as pd
data = pd.Series([1,5,3,2,4], index=['A','E','C','B','D'])
# Sorting the data by index
sorted_data = data.sort_index()
print(sorted_data)
Output:
# A 1
# B 2
# C 3
# D 4
# E 5
# dtype: int64
Our series data is sorted by index in ascending order. You can also sort it in descending order by passing ascending=False
to the sort_index()
function.
sort_values()
The sort_values()
function is used to sort the data frame or series by its values in either ascending or descending order.
Let's understand sort_values()
with a simple DataFrame example:
df = pd.DataFrame({
'Name': ['Mike', 'Brad', 'Chan', 'Alex'],
'Age': [34, 30, 22, 41]
})
# Sorting the data by Age
sorted_df = df.sort_values(by='Age')
print(sorted_df)
Output:
# Name Age
# 2 Chan 22
# 1 Brad 30
# 0 Mike 34
# 3 Alex 41