Change language

Reset index in Pandas Dataframe

| | | |

When we look at a smaller data frame, it may still contain the row index of the original data frame. If the original index is numbers , we now have indices that are not contiguous. Well, pandas have reset_index () . So, to reset the index to the default integer index starting at 0, we can simply use .reset_index () .

So let’s see how we can reset the DataFrame’s index.

Check out the original DataFrame first.

# 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 dictionary to DataFrame

df = pd.DataFrame (data)

 
df

Exit:

Example # 1: Create your own index without removing the default index.

# 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’ , ’ Ph d’ , ’10th’ ]}

 

index = { ’a’ , ’b’ , ’ c’ , ’d’ , ’ e’ }

 
# Convert dictionary to DataFrame

df = pd.DataFrame (data, index)

 
# Make your own index as an index
# In this case, the default index exists

df.reset_index (inplace = True )

 
df

Output:

Example # 2: Create your own index and drop the default index.

# 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’ , ’All ahabad’ , ’Kannauj’ , ’Noida’ ],

  ’Qualification’ : [ ’ Msc’ , ’MA’ , ’ MCA’ , ’Phd’ , ’ 10th’ ]}

 
# Create own index

index = { ’a’ , ’ b’ , ’ c’ , ’d’ , ’ e’ }

 
# Convert dictionary to DataFrame
# Create your own index and drop the default index

df = pd.DataFrame (data, index)

 
df

Exit:

Example 3: Reset your own index and make the default index as the index.

# 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 dictionary to DataFrame

df = pd.DataFrame (data, index)

 
# delete custom index with default index

df.reset_index (inplace = True , drop = True )

  
df

Output:

Example # 4: Make data column as index deleting the default index.

# 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 your own index

index = { ’a’ , ’b’ , ’ c’ , ’d’ , ’ e’ }

 
# Convert dictionary to DataFrame

df = pd.DataFrame (data, index )

 
# set the index of any column of our DF and
# remove default index

df.set_index ([ ’ Age’ ], inplace = True )

 
df

Output:

Example 5. Creating a data column as an index without dropping the y index silence.

# 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 your own index

index = { ’a’ , ’ b’ , ’c’ , ’ d ’ , ’ e’ }

 
# Convert dictionary to DataFrame

df = pd.DataFrame (data, i ndex)

 
# set any column as index
# Here we set the age column as index

df.set_index ([ ’Age’ ], inplace = True )

 
# reset index without deleting default index

df.reset_index (level = [ ’Age’ ], inplace = True )

 
df

Exit d:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method