Wednesday, November 27, 2024
Google search engine
HomeLanguagesReset Index in Pandas Dataframe

Reset Index in Pandas Dataframe

Let’s discuss how to reset the index in Pandas DataFrame. Often We start with a huge dataframe in Pandas and after manipulating/filtering the dataframe, we end up with a 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 is numbers, now we have indexes that are not continuous. ‘

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.

Creating 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

Example #1: Creating Index Make Own Index Without Removing Default 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'] }
 
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

Example #2: Make Own Index and Removing Default 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

Example 3: Reset own index and make default index as 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
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

Example #4: Make a column of dataframe as index with remove default 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
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

Example 5: Make a column of dataframe as an index without remove default 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
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

RELATED ARTICLES

Most Popular

Recent Comments