Pandas drop by index – Python Pandas : How to drop rows in DataFrame by index labels

How to drop rows in DataFrame by index labels in Python ?

Pandas drop by index: In this article we are going to learn how to delete single or multiple rows from a Dataframe.

For this we are going to use the drop( ) function.

Syntax - DataFrame.drop( labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise' )

Where, the function accepts name/series of names in the label and deletes the rows or columns it points to. The axis is used to alter between rows and columns, 0 means rows and 1 means columns(default value is 0).

Also we have to pass inplace = True if we want the modified values to be updated in our dataframe object, as the drop( ) function returns the modified values into a new Dataframe object. To explain this properly we will be using inplace = True in all our programs.

We are going to use the following dataset as example

      Name          Age       Location       Country
a     Jill               16         Tokyo             Japan
b    Phoebe        38        New York        USA
c     Kirti             39         New York        USA
d     Veena         40         Delhi               India
e     John           54          Mumbai         India
f     Michael       21         Tokyo              Japan

Deleting a single Row in DataFrame by Row Index Label :

To delete a single row by the label we can just pass the label into the function.

Here let’s try to delete‘b’ row.

#program :

import numpy as np
import pandas as pd

#Examole data
students = [('Jill',   16,  'Tokyo',     'Japan'),
('Phoebe', 38,  'New York',  'USA'),
('Kirti',  39,  'New York',  'USA'),
('Veena',  40,  'Delhi',     'India'),
('John',   54,  'Mumbai',    'India'),
("Michael",21,  'Tokyo',     'Japan')]

#Creating an object of dataframe class
dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'Location' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])
#Deleting 'b' row
dfObj.drop('b',inplace=True)
print(dfObj)

Output :

        Name     Age      Location       Country
a        Jill          16       Tokyo            Japan
c        Kirti        39       New York      USA
d      Veena      40       Delhi             India
e      John         54       Mumbai        India
f     Michael     21       Tokyo            Japan

Deleting Multiple Rows in DataFrame by Index Labels :

To delete multiple rows by their labels we can just pass the labels into the function inside a square bracket [ ].

Here let’s try to delete 'a' and 'b' row.

#program :

import numpy as np
import pandas as pd

#Examole data
students = [('Jill',   16,  'Tokyo',     'Japan'),
('Phoebe', 38,  'New York',  'USA'),
('Kirti',  39,  'New York',  'USA'),
('Veena',  40,  'Delhi',     'India'),
('John',   54,  'Mumbai',    'India'),
("Michael",21,  'Tokyo',     'Japan')]

#Creating an object of dataframe class
dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'Location' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])

#Deleting 'a' and 'b' row
dfObj.drop(['a','b'],inplace=True)
print(dfObj)
Output :
      Name       Age     Location       Country
c     Kirti          39      New York      USA
d     Veena      40      Delhi             India
e     John         54     Mumbai         India
f     Michael     21     Tokyo             Japan

Deleting Multiple Rows by Index Position in DataFrame :

To delete multiple rows we know the index position, however, the function drop( ) doesn’t take indices as parameters. So we create the list of labels and pass them into the drop( ) function. Let’s try deleting the same rows again but by index.

#program :

import numpy as np
import pandas as pd

#Examole data
students = [('Jill',   16,  'Tokyo',     'Japan'),
('Phoebe', 38,  'New York',  'USA'),
('Kirti',  39,  'New York',  'USA'),
('Veena',  40,  'Delhi',     'India'),
('John',   54,  'Mumbai',    'India'),
("Michael",21,  'Tokyo',     'Japan')]

#Creating an object of dataframe class
dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'Location' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])

#Deleting 1st and 2nd row
dfObj.drop([dfObj.index[0] , dfObj.index[1]],inplace=True)
print(dfObj)
Output :
      Name      Age     Location     Country
c     Kirti          39     New York     USA
d     Veena      40     Delhi            India
e     John         54    Mumbai        India
f     Michael     21     Tokyo          Japan

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Remove Contents from a Dataframe

Leave a Comment