How to Sort an Index in Pandas DataFrame

You can sort an index in Pandas DataFrame:

(1) In an ascending order:

df = df.sort_index()

(2) In a descending order:

df = df.sort_index(ascending=False)

Let’s see how to sort an index by reviewing an example.

The Example

To start, let’s create a simple DataFrame:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [1100,150,500,350,100,300,120]
        }

df = pd.DataFrame(data, columns = ['Product','Price'])

print(df)

By default, the index is sorted in an ascending order:

Default Index

Let’s replace the default index values with the following unsorted values:

index = ['Item_D','Item_B','Item_A','Item_C','Item_F','Item_G','Item_E']

The goal is to sort the above values in an ascending order.

The current DataFrame with the new unsorted index is as follows:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [1100,150,500,350,100,300,120]
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_D','Item_B','Item_A','Item_C','Item_F','Item_G','Item_E'])

print(df)

As you can see, the current index values are unsorted:

How to Sort an Index in Pandas DataFrame

Sort the Index in an Ascending Order in Pandas DataFrame

In order to sort the index in an ascending order, you’ll need to add the following syntax to the code:

df = df.sort_index()

So the complete Python code to sort the index is:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [1100,150,500,350,100,300,120]
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_D','Item_B','Item_A','Item_C','Item_F','Item_G','Item_E'])

df = df.sort_index()

print(df)

Notice that the index is now sorted in an ascending order:

Sort an Index in Pandas DataFrame

Sort the Index in a Descending Order in Pandas DataFrame

What if you’d like to sort the index in a descending order?

In that case, you’ll need to add the following syntax:

df = df.sort_index(ascending=False)

Here is the complete Python code:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [1100,150,500,350,100,300,120]
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = ['Item_D','Item_B','Item_A','Item_C','Item_F','Item_G','Item_E'])

df = df.sort_index(ascending=False)

print(df)

You’ll now see that the index is sorted in a descending order:

Index descending order

Index is Numeric

So far, the index sorted was non-numeric.

The same concept would apply if the index values are numeric:

index = [400,200,100,300,600,700,500]

Let’s sort the index in an ascending order:

import pandas as pd

data = {'Product': ['Computer','Printer','Monitor','Desk','Phone','Tablet','Scanner'],
          'Price': [1100,150,500,350,100,300,120]
        }

df = pd.DataFrame(data, columns = ['Product','Price'], index = [400,200,100,300,600,700,500])

df = df.sort_index()

print(df)

The index will be sorted as follows:

Sort Index Pandas DataFrame

You may visit the Pandas Documentation to learn more about df.sort_index.