How to Reset the Index of a Pandas Dataframe Object in Python

In this article, we show how to reset the index of a pandas dataframe object in
Python.

So, say you have a pandas dataframe object with 4 rows with indexes 'A', 'B', 'C', and 'D'.

However, you want to reset the index to the default integer index beginning at 0, then going to
1,2,3...

How can we do this to a pandas dataframe in Python?

Well, pandas has built-in reset_index() function. So to reset the index to the
default integer index beginning at 0, you can simply use the built-in reset_index() function.

This will make the integer index the default index and take the existing index
and make it a column.

So let's see how this works in the following code shown below.

So let's now go over the code.

So we first have to import the pandas module. We do this with the line,
import pandas as pd.

as pd means that we can reference the pandas module with pd instead of writing
out the full pandas each time.

We import rand from numpy.random, so that we can populate the DataFrame with random values.
In other words, we won't need to manually create the values in the table. The randn function
will populate it with random values.

We create a variable, dataframe1, which we set equal to, pd.DataFrame(randn(4,3),['A','B','C','D',],['X','Y','Z'])

This creates a DataFrame object with 4 rows and 3 columns.

The rows are 'A', 'B', 'C', and 'D'.

The columns are 'W', 'X', and 'Y'.

After we output the dataframe1 object, we get the DataFrame object with all the rows and columns, which you can see above.

We then use the built-in reset_index() function to set the index to the default integer index beginning at 0.

The previous index then becomes another column of the dataframe.

You must specify the statement, inplace=True, in order for the change to be permanently made of setting the
index to the default integer index.

And this is how we can reset the index of a pandas dataframe object in Python.