
In this post, you will learn –
1 . How to sort a dataframe by index in ascending order
2 . How to sort a dataframe by index in descending order
3. How to sort a dataframe by index which has multiindex.
1 . How to sort a dataframe by index in ascending order –
Let’s first read a dataset –
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv")
df.head()

To sort a dataframe by index in ascending order, you have to write –
df.sort_index()

By default, the ascending parameter set to True, means it will sort the index in ascending order.
2 . How to sort a dataframe by index in descending order
To sort a dataframe by index in descending order, you need to set the ascending parameter to False.
df.sort_index(ascending=False)

3. How to sort a dataframe by index which has multiindex
First, let’s create a multiindex dataframe to work with
df.set_index(['Age','Net Sales'], inplace=True)
df.head()

Now, to sort both Age and Net Sales in ascending order, you have to write
df.sort_index(level=['Age','Net Sales'])

And to sort Age in descending order and Net Sales in ascending order, you have to write –
df.sort_index(level=['Age','Net Sales'], ascending=[False, True])
