Sling Academy
Home/Pandas/Pandas – DataFrame.sort_index() method (6 examples)

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

Last updated: February 24, 2024

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.

Next Article: Pandas DataFrame nlargest() and nsmallest() methods (5 examples)

Previous Article: Using DataFrame.sort_values() method in Pandas (5 examples)

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)