Sort an Index in Pandas DataFrame

You can sort an index in Pandas DataFrame:

(1) In an ascending order:

df.sort_index(inplace=True)

(2) In a descending order:

df.sort_index(inplace=True, ascending=False)

The Example

To start, 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)

print(df)

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

    Product  Price
0  Computer   1100
1   Printer    150
2   Monitor    500
3      Desk    350
4     Phone    100
5    Tablet    300
6   Scanner    120

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 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, 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:

         Product  Price
Item_D  Computer   1100
Item_B   Printer    150
Item_A   Monitor    500
Item_C      Desk    350
Item_F     Phone    100
Item_G    Tablet    300
Item_E   Scanner    120

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.sort_index(inplace=True)

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, index = ['Item_D','Item_B','Item_A','Item_C','Item_F','Item_G','Item_E'])

df.sort_index(inplace=True)

print(df)

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

         Product  Price
Item_A   Monitor    500
Item_B   Printer    150
Item_C      Desk    350
Item_D  Computer   1100
Item_E   Scanner    120
Item_F     Phone    100
Item_G    Tablet    300

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.sort_index(inplace=True, 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, index=["Item_D", "Item_B", "Item_A", "Item_C", "Item_F", "Item_G", "Item_E"]
)

df.sort_index(inplace=True, ascending=False)

print(df)

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

         Product  Price
Item_G    Tablet    300
Item_F     Phone    100
Item_E   Scanner    120
Item_D  Computer   1100
Item_C      Desk    350
Item_B   Printer    150
Item_A   Monitor    500

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]

To 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, index=[400, 200, 100, 300, 600, 700, 500])

df.sort_index(inplace=True)

print(df)

The index will be sorted as follows:

      Product  Price
100   Monitor    500
200   Printer    150
300      Desk    350
400  Computer   1100
500   Scanner    120
600     Phone    100
700    Tablet    300

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

Leave a Comment