Pandas – DataFrame.sort_index() method (6 examples)

Updated: February 24, 2024 By: Guest Contributor Post a comment

Introduction

In the world of data analysis with Python, Pandas stands as a pillar for manipulating and analyzing data. Among its powerful features, the sort_index() method is a versatile tool for sorting data frames based on their indexes. This tutorial explores six examples of how to use sort_index() method, ranging from basic to advanced scenarios.

What is sort_index() Used for?

The sort_index() method in Pandas is used to sort a DataFrame or Series by its index labels. This is beneficial for quickly reorganizing your data in ascending or descending order based on the index, facilitating easier data analysis and visualization.

Example 1: Basic Sorting of DataFrame

Let’s start with the most basic example of sorting a DataFrame by its index.

import pandas as pd

df = pd.DataFrame({'A': [2, 1, 3], 'B': [5, 4, 6]}, index=['b', 'a', 'c'])
print("Original DataFrame:\n", df)

df_sorted = df.sort_index()
print("Sorted DataFrame:\n", df_sorted)

Output:

Original DataFrame:
    A  B
b  2  5
a  1  4
c  3  6

Sorted DataFrame:
    A  B
a  1  4
b  2  5
c  3  6

Example 2: Sorting Descending

To sort the index in descending order, use the ascending=False argument.

df_sorted_desc = df.sort_index(ascending=False)
print("Sorted in descending order:\n", df_sorted_desc)

Output:

Sorted in descending order:
    A  B
c  3  6
b  2  5
a  1  4

Example 3: Sorting by MultiIndex

When working with a DataFrame that has multiple levels of indexes (multi-index), sort_index() can sort these layers easily.

index = pd.MultiIndex.from_tuples([('a', 1), ('a', 2), ('b', 1), ('b', 2)])
df_multi = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}, index=index)
print("Original MultiIndex DataFrame:\n", df_multi)

df_multi_sorted = df_multi.sort_index()
print("Sorted MultiIndex DataFrame:\n", df_multi_sorted)

Output:

Original MultiIndex DataFrame:
      A  B
a 1  1  5
  2  2  6
b 1  3  7
  2  4  8

Sorted MultiIndex DataFrame:
      A  B
a 1  1  5
  2  2  6
b 1  3  7
  2  4  8

Example 4: Sorting with Missing Index Labels

What happens when your DataFrame index has missing values? Let’s explore how sort_index() handles it.

df_missing = pd.DataFrame({'A': [1, 2, 3]}, index=[1, None, 2])
print("DataFrame with missing index:\n", df_missing)

df_missing_sorted = df_missing.sort_index()
print("DataFrame sorted by index with missing values handled:\n", df_missing_sorted)

Output:

DataFrame with missing index:
       A
1    1
NaN  2
2    3

DataFrame sorted by index with missing values handled:
       A
NaN  2
1    1
2    3

Example 5: Sorting by Index and Column Simultaneously

Advanced usage of sort_index() allows sorting by both the index and a column (or columns) concurrently. This involves first using sort_index(), then applying sort_values().

df = pd.DataFrame({'A': [2, 1, 3], 'B': [5, 4, 6]}, index=[3, 1, 2])
print("Original DataFrame:\n", df)

# First sort by index
df_index_sorted = df.sort_index()
print("Index-sorted DataFrame:\n", df_index_sorted)

# Then sort by column A
df_final_sorted = df_index_sorted.sort_values(by='A')
print("DataFrame sorted by index and column A:\n", df_final_sorted)

Output:

Original DataFrame:
     A  B
3  2  5
1  1  4
2  3  6

Index-sorted DataFrame:
     A  B
1  1  4
2  3  6
3  2  5

DataFrame sorted by index and column A:
     A  B
1  1  4
3  2  5
2  3  6

Example 6: In-Place Sorting

In-place sorting modifies the original DataFrame directly. This means you won’t need to assign the result to a new variable. Let’s see how it’s done.

df = pd.DataFrame({'A': [3, 1, 2], 'B': [6, 4, 5]}, index=['c', 'a', 'b'])
df.sort_index(inplace=True)
print("In-place sorted DataFrame:\n", df)

Output:

In-place sorted DataFrame:
    A  B
a  1  4
b  2  5
c  3  6

Conclusion

The sort_index() method in Pandas is incredibly versatile and easy to use, catering to a wide variety of data sorting needs. Whether your data is straightforward or complex with multi-indexing, sort_index() offers a streamlined approach to organizing it, facilitating clearer analyses and insights.