
When you do various operations on pandas dataframe, the index get out of order. To bring them in right order we use the reset_index method in pandas.
How to reset index in pandas –
Let’s first read a dataset to work with –
import pandas as pd
df = pd.read_csv(
"https://raw.githubusercontent.com/bprasad26/lwd/master/data/titanic.csv"
)
df.head()

We can see that there are some missing values in the cabin column. Let’s drop those missing values to make the index out of order. If you not familiar with how the DataFrame.dropna() method works, then read this post – Pandas – dropna() method -Handle Missing Values in python
df.dropna(inplace=True)
df.head()

Now, you can see that the index of the dataframe is out of order, it’s now 1, 3, 6, 10, 11 etc.
To order the index of the dataframe, you have to write –
df.reset_index()

Now, you can see that the index has been ordered from 0 to 182. But you can see that the previous index was added as a column in the dataframe. Sometimes it is useful like when you have a time series data in the index and you want to treat them as a column. But in this case it is not, so to stop the index being added as a column in the dataframe, we need to use the drop parameter of the reset_index.
df.reset_index(drop=True)

How to reset Multi-index in pandas –
Sometimes you might also have multi-index. Let’s create one. For more info read this – Pandas – set_index() – How to set a column as index
df.set_index(["Survived", "Pclass"], inplace=True)
df.head()

Now to reset the index, one option is to reset the index and bring both the survived
and Pclass
as a column.
df.reset_index()

But if you want to only bring the Survived
as a column while resetting the index, then you can use the level parameter of reset_index.
df.reset_index(level="Survived")

Related Posts –
1 . Pandas – set_index() – How to set a column as index
2 . Pandas – sort_index() – How to sort dataframe by index