Reset Index in Pandas Dataframe

04 Dec, 2018
Let’s discuss how to reset index in Pandas DataFrame. Often We start with a huge dataframe in Pandas and after manipulating/filtering the dataframe, we end up with much smaller dataframe.
When we look at the smaller dataframe, it might still carry the row index of the original dataframe. If the original index are numbers, now we have indexes that are not continuous. Well, pandas has reset_index() function. So to reset the index to the default integer index beginning at 0, We can simply use the reset_index() function.
So let’s see the different ways we can reset the index of a DataFrame.
First see original DataFrame.
# Import pandas packageimport pandas as pd    # Define a dictionary containing employee datadata = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj', 'Geeku'],        'Age':[27, 24, 22, 32, 15],        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj', 'Noida'],        'Qualification':['Msc', 'MA', 'MCA', 'Phd', '10th'] }  # Convert the dictionary into DataFrame df = pd.DataFrame(data)  df
Output:
 Example #1: Make Own Index Without Removing Default index.
# Import pandas packageimport pandas as pd    # Define a dictionary containing employee datadata = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj', 'Geeku'],        'Age':[27, 24, 22, 32, 15],        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj', 'Noida'],        'Qualification':['Msc', 'MA', 'MCA', 'Phd', '10th'] }  index = {'a', 'b', 'c', 'd', 'e'}  # Convert the dictionary into DataFrame df = pd.DataFrame(data, index)  # Make Own Index as index# In this case default index is exist df.reset_index(inplace = True)  df
Output: Example #2: Make Own Index and Removing Default index.
# Import pandas packageimport pandas as pd    # Define a dictionary containing employee datadata = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj', 'Geeku'],        'Age':[27, 24, 22, 32, 15],        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj', 'Noida'],        'Qualification':['Msc', 'MA', 'MCA', 'Phd', '10th'] }  # Create own indexindex = {'a', 'b', 'c', 'd', 'e'}  # Convert the dictionary into DataFrame # Make Own Index and Removing Default indexdf = pd.DataFrame(data, index)  df
Output:
 Example 3: Reset own index and make default index as index.
# Import pandas packageimport pandas as pd    # Define a dictionary containing employee datadata = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj', 'Geeku'],        'Age':[27, 24, 22, 32, 15],        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj', 'Noida'],        'Qualification':['Msc', 'MA', 'MCA', 'Phd', '10th'] }  # Create own indexindex = {'a', 'b', 'c', 'd', 'e'}  # Convert the dictionary into DataFrame df = pd.DataFrame(data, index)  # remove own index with default indexdf.reset_index(inplace = True, drop = True)  df
Output: Example #4: Make a column of dataframe as index with remove default index.
# Import pandas packageimport pandas as pd    # Define a dictionary containing employee datadata = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj', 'Geeku'],        'Age':[27, 24, 22, 32, 15],        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj', 'Noida'],        'Qualification':['Msc', 'MA', 'MCA', 'Phd', '10th'] }  # Create own indexindex = {'a', 'b', 'c', 'd', 'e'}  # Convert the dictionary into DataFrame df = pd.DataFrame(data, index)  # set index any column of our DF and# remove default indexdf.set_index(['Age'], inplace = True)  df
Output: Example 5: Make a column of dataframe as index without remove default index.
# Import pandas packageimport pandas as pd    # Define a dictionary containing employee datadata = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj', 'Geeku'],        'Age':[27, 24, 22, 32, 15],        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj', 'Noida'],        'Qualification':['Msc', 'MA', 'MCA', 'Phd', '10th'] }  # Create own indexindex = {'a', 'b', 'c', 'd', 'e'}  # Convert the dictionary into DataFrame df = pd.DataFrame(data, index)  # set any column as index# Here we set age column as indexdf.set_index(['Age'], inplace = True)  # reset index without removing default indexdf.reset_index(level =['Age'], inplace = True)  df
Output:
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Technical Scripter 2018
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Convert integer to string in Python
Python OOPs Concepts
Introduction To PYTHON