Home »
Python »
Python Programs
Pandas: Reset index is not taking effect
Learn, how to fix the reset index is not taking effect issue in Python Pandas?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose we are given the dataframe on which if we apply DataFrame.reset_index() method, it will reset the index of the dataframe but if we display the result the index will be the same.
Solution of "Reset index is not taking effect"
Pandas DataFrame.reset_index() method is used to reset the index or a level of it. It reset the index of the dataframe and use the default one in the state. This method can remove one or more levels if the dataframe has a multi-index.
This is because DataFrame.reset_index() method by default does not modify the dataframe. It returns a new dataframe with the reset index. If we want to modify the original, we need to use the 'inplace' argument.
Let us understand with the help of an example,
Python program to fix the reset index is not taking effect issue
# Importing pandas
import pandas as pd
# Creating a dictionary
d = {
'A':[10000,10001,10002,10003,10004],
'B':['a','b','c','d','e']
}
# Creating a dataframe
df = pd.DataFrame(d)
# setting index
df = df.set_index(df['A'])
# Display df
print("New index:\n",df,"\n")
# Reset index
del df['A']
df.reset_index(inplace=True)
# Display new df
print("Retrained df:\n",df,"\n")
Output
The output of the above program is:
Python Pandas Programs »