Drop index column of Pandas DataFrame Python

In this post, we are going to learn How to Drop index column of Pandas DataFrame Python. Sometimes we may need to drop the index column of the pandas dataframe in Python. The Pandas data frames index can’t be dropped instead can be reset by using pandas.dataframe built-in reset() function.

Pandas reset_index() method


The Pandas dataframe reset_index() of dataframe single or multiple indexes if dataframe is multi-index.

Syntax

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

Parameters

  • drop: It reset the index of the dataframe to default index.
  • inplace :It modified the original dataframe in place does not create object of dataframe

1. reset_index() to Drop index column of Pandas DataFrame Python


The pandas. dataframe reset_index() reset the index column of the dataframe with the default sequential list of indexes. Let us understand with the below Python program how to remove the index of pandas dataframe.

In this python program example, we are dropping the index of the pandas dataframe by using the reset_index() method of dataframe.

Python Program to drop index of pandas DataFrame

import pandas as pd
   
  
dfobj = pd.DataFrame({
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic'],
},index =['Row_1','Row_2','Row_3','Row_4'])

print('original df:\n',dfobj)

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

print('\ndataframe after rest index:\n',dfobj)



Output

original df:
         Name  Marks Subject
Row_1   Jack    100    Math
Row_2   Rack    100    Math
Row_3    Max    100   Music
Row_4  David    100  Physic

dataframe after rest index:
     Name  Marks Subject
0   Jack    100    Math
1   Rack    100    Math
2    Max    100   Music
3  David    100  Physic


2. Drop index column of Pandas DataFrame Python using set_index()


The dataframe set_index() method reset the index column of the pandas dataframe with the values of another column that passed as an argument.

In this Python program, we want to reset the dataframe default index with the “name” column. As we can see in the output the dataframe default column index has been overriding with “name” column values.

Python Program to drop index of pandas DataFrame

import pandas as pd
   
  
dfobj = pd.DataFrame({
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic'],
},index =['Row_1','Row_2','Row_3','Row_4'])

print('original df:\n',dfobj)

result_df = dfobj.set_index('Name')

print('dataframe after rest index:')
print(result_df)

Output

original df:
         Name  Marks Subject
Row_1   Jack    100    Math
Row_2   Rack    100    Math
Row_3    Max    100   Music
Row_4  David    100  Physic
dataframe after rest index:
       Marks Subject
Name                
Jack     100    Math
Rack     100    Math
Max      100   Music
David    100  Physic

Summary

In this post, we have used pandas. dataframe reset_index() and set_index() method to remove or drop index column of pandas dataframe