Pandas is a popular library for data manipulation in Python. One of the common tasks in data analysis is resetting an index. In this article, we will discuss how to reset a pandas index and provide examples to help you understand the process.

Why Reset a Pandas Index?

Resetting an index in Pandas is useful when you want to change the order of the rows or when the current index is not meaningful or unique. For example, if you have a DataFrame with a non-unique index, resetting the index can create a new unique index based on the order of the rows.

Loading a Sample Dataframe

Feel free to copy the code below if you’d like to follow along with an example dataframe. To print a small dataframe in its totality, we will load it. Running this dataframe should not cause any significant speed issues, but they will become more apparent as your dataset expands.

				
					import pandas as pd

# create a dictionary with sample data
data = {'Name': ['John', 'Emily', 'Charlie', 'Lisa'],
        'Age': [25, 28, 30, 35],
        'City': ['New York', 'London', 'Paris', 'Sydney']}

# create a pandas dataframe from the dictionary
df = pd.DataFrame(data)

# print the dataframe
print(df)

				
			

Output:

				
					#      Name  Age      City
# 0     John   25  New York
# 1    Emily   28    London
# 2  Charlie   30     Paris
# 3     Lisa   35    Sydney
				
			

This will create a dataframe with columns for Name, Age, and City, and rows for each of the sample data entries. You can modify the data dictionary to include your own sample data, and pandas will automatically create the dataframe accordingly.

How to Reset an Index in Pandas

To reset an index in pandas, you can use the reset_index() method. This method adds a new index column to the dataframe and resets the existing index.

Here’s an example:

				
					import pandas as pd

# create a sample dataframe with a custom index
data = {'Name': ['John', 'Emily', 'Charlie', 'Lisa'],
        'Age': [25, 28, 30, 35],
        'City': ['New York', 'London', 'Paris', 'Sydney']}
df = pd.DataFrame(data, index=['A', 'B', 'C', 'D'])
print(df)

# reset the index
df = df.reset_index()

# print the updated dataframe
print(df)

				
			

Output:

				
					#      Name  Age      City
# A     John   25  New York
# B    Emily   28    London
# C  Charlie   30     Paris
# D     Lisa   35    Sydney
#  index     Name  Age      City
# 0     A     John   25  New York
# 1     B    Emily   28    London
# 2     C  Charlie   30     Paris
# 3     D     Lisa   35    Sydney
				
			

In this example, the original dataframe has a custom index of ['A', 'B', 'C', 'D']. After calling reset_index(), the dataframe is updated to have a new index starting from 0 and a new column for the previous index values.

How to Reset a Pandas Index In-Place

Reassigning a dataframe when resetting its index is not always desired. With the inplace= parameter, you can therefore reset a Pandas index in place.

				
					import pandas as pd

# create a sample dataframe with a custom index
data = {'Name': ['John', 'Emily', 'Charlie', 'Lisa'],
        'Age': [25, 28, 30, 35],
        'City': ['New York', 'London', 'Paris', 'Sydney']}
df = pd.DataFrame(data, index=['A', 'B', 'C', 'D'])
print(df)

# reset the index in-place
df.reset_index(drop=True, inplace=True)

# print the updated dataframe
print(df)

				
			

Output:

				
					#      Name  Age      City
#A     John   25  New York
#B    Emily   28    London
#C  Charlie   30     Paris
#D     Lisa   35    Sydney
#      Name  Age      City
#0     John   25  New York
#1    Emily   28    London
#2  Charlie   30     Paris
#3     Lisa   35    Sydney
				
			

In this example, the reset_index() method is called with the drop=True argument to drop the original index column. The resulting dataframe has a new index starting from 0, and the previous index column is no longer present.

Note that if you don’t include the drop=True argument, the previous index column will be included in the resulting dataframe as a new column.

How to Reset a Multi-Level Pandas Index

To reset a multi-level pandas index, you can use the reset_index() method with the level argument. Here’s an example:

				
					import pandas as pd

# create a sample dataframe with a multi-level index
data = {'Name': ['John', 'Emily', 'Charlie', 'Lisa'],
        'Age': [25, 28, 30, 35],
        'City': ['New York', 'London', 'Paris', 'Sydney']}
index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)])
df = pd.DataFrame(data, index=index)
print(df)

# reset the multi-level index
df = df.reset_index(level=[0, 1])

# print the updated dataframe
print(df)

				
			

Output:

				
					#        Name  Age      City
# A 1     John   25  New York
#   2    Emily   28    London
# B 1  Charlie   30     Paris
#   2     Lisa   35    Sydney
#  level_0  level_1     Name  Age      City
# 0       A        1     John   25  New York
# 1       A        2    Emily   28    London
# 2       B        1  Charlie   30     Paris
# 3       B        2     Lisa   35    Sydney
				
			

In this example, the reset_index() method is called with the level=[0, 1] argument to reset both levels of the multi-level index. The resulting dataframe has a new integer index and the previous levels of the multi-level index are added as new columns.

Note that you can also use the drop argument with the reset_index() method to drop one or more levels of the multi-level index. For example, if you want to drop the second level of the index, you can use df.reset_index(level=1, drop=True).

Wrap up

To learn more about the Pandas reset_index() method, check out the:
https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.reset_index.html


Thanks for reading. Happy coding!