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

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically