Open In App
Related Articles

Reset Index in Pandas Dataframe

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Let’s discuss how to reset the index in Pandas DataFrame. Often We start with a huge data frame in Pandas and after manipulating/filtering the data frame, we end up with a much smaller data frame. When we look at the smaller data frame, it might still carry the row index of the original data frame. If the original index is numbers, now we have indexes that are not continuous.

Reset Index Syntax

Syntax :

DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill=”)

  • Parameters:
    • level: Specifies multi-level index levels to reset.
    • drop: Discards current index if True; adds as a new column if False.
    • inplace: Modifies DataFrame in place if True; returns a new DataFrame if False.
    • col_level: Specifies which level of multi-level columns to reset.
    • col_fill: Fills missing values in columns’ levels.
  • Return Type: Returns a new DataFrame if inplace is False; None if inplace is True

Well, pandas have 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.

What is Reset Index ?

In Python programming language and the pandas library, the reset_index method is used to reset the index of a data frame. When you perform operations on a DataFrame in pandas, the index of the DataFrame may change or become unordered. The reset_index method allows you to reset the index to the default integer-based index and reset the index in Pandas DataFrame optionally removing the current index.

Reset Index in Pandas Dataframe

There are various methods with the help of which we can Reset the Index in Pandas Dataframe, we are explaining some generally used methods with examples.

  • Create Own Index Without Removing Default Index
  • Create your Own Index and Remove the Default Index
  • Reset Own Index and Create Default Index as Index
  • Make a Column of Dataframe as Index and Removing Default Index
  • Make a Column of Dataframe as an Index Without Removing Index

Creating Pandas DataFrame

Here we are creating a sample Pandas Dataframe:

Python3

# Import pandas package
import pandas as pd
     
# Define a dictionary containing employee data
data = {'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)
print(df)

                    

Output:

     Name  Age    Address Qualification
0     Jai   27      Delhi           Msc
1  Princi   24     Kanpur            MA
2  Gaurav   22  Allahabad           MCA
3    Anuj   32    Kannauj           Phd
4   Geeku   15      Noida          10th

Create Own Index without Removing Default Index

In this example below code uses the pandas library to create a DataFrame from employee data. It defines a dictionary, sets a custom index, converts it to a DataFrame, resets the index, and prints the result.

Python3

# Import pandas package
import pandas as pd
     
# Define a dictionary containing employee data
data = {'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)
 
print(df)

                    

Output:

  index    Name  Age    Address Qualification
0     a     Jai   27      Delhi           Msc
1     b  Princi   24     Kanpur            MA
2     c  Gaurav   22  Allahabad           MCA
3     d    Anuj   32    Kannauj           Phd
4     e   Geeku   15      Noida          10th

Create your Own Index and Remove Default Index

In this example below code uses the pandas library to create a DataFrame from employee data stored in a dictionary. It sets a custom index (‘a’ to ‘e’) and then prints the resulting DataFrame, where the custom index replaces the default numeric index.

Python3

# Import pandas package
import pandas as pd
   
# Define a dictionary containing employee data
data = {'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 index
index = ['a', 'b', 'c', 'd', 'e']
 
# Convert the dictionary into DataFrame
# Make Own Index and Removing Default index
df = pd.DataFrame(data, index)
 
print(df)

                    

Output:

     Name  Age    Address Qualification
a     Jai   27      Delhi           Msc
b  Princi   24     Kanpur            MA
c  Gaurav   22  Allahabad           MCA
d    Anuj   32    Kannauj           Phd
e   Geeku   15      Noida          10th

Reset Own Index and Create Default Index as Index

In this example below code creates a Pandas DataFrame from a dictionary of employee data with a custom index (‘a’ to ‘e’). Afterward, it resets the index, replacing the custom index with the default numeric index, and then prints the resulting frame.

Python3

# Import pandas package
import pandas as pd
   
# Define a dictionary containing employee data
data = {'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 index
index = ['a', 'b', 'c', 'd', 'e']
 
# Convert the dictionary into DataFrame
df = pd.DataFrame(data, index)
 
# remove own index with default index
df.reset_index(inplace = True, drop = True)
 
print(df)

                    

Output :

     Name  Age    Address Qualification
0     Jai   27      Delhi           Msc
1  Princi   24     Kanpur            MA
2  Gaurav   22  Allahabad           MCA
3    Anuj   32    Kannauj           Phd
4   Geeku   15      Noida          10th

Make a Column as Index and Removing Default Index

In this example below code creates a Pandas DataFrame from employee data, sets a custom index, and then changes the index to the ‘Age’ column while removing the default numeric index. The final data frame is printed twice.

Python3

# Import pandas package
import pandas as pd
   
# Define a dictionary containing employee data
data = {'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 index
index = ['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 index
df.set_index(['Age'], inplace = True)
 
print(df)

                    

Output:

       Name    Address Qualification
Age                                 
27      Jai      Delhi           Msc
24   Princi     Kanpur            MA
22   Gaurav  Allahabad           MCA
32     Anuj    Kannauj           Phd
15    Geeku      Noida          10th

Make a Column of Dataframe as an Index Without Removing Index

In this example below code creates a DataFrame from employee data, initially using a custom index. Then, it sets the ‘Age’ column as the index, resets the index without removing the default numeric index, and finally prints the resulting DataFrame.

Python3

# Import pandas package
import pandas as pd
   
# Define a dictionary containing employee data
data = {'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 index
index = ['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 index
df.set_index(['Age'], inplace = True)
 
# reset index without removing default index
df.reset_index(level =['Age'], inplace = True)
 
print(df)

                    

Output:

   Age    Name    Address Qualification
0   27     Jai      Delhi           Msc
1   24  Princi     Kanpur            MA
2   22  Gaurav  Allahabad           MCA
3   32    Anuj    Kannauj           Phd
4   15   Geeku      Noida          10th


Last Updated : 30 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads