In this article we will discuss how to delete single or multiple rows from a DataFrame object.

DataFrame provides a member function drop() i.e.

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

It accepts a single or list of label names and deletes the corresponding rows or columns (based on value of axis parameter i.e. 0 for rows or 1 for columns). As default value for axis is 0, so for dropping rows we need not to pass axis.

Also, by default drop() doesn’t modify the existing DataFrame, instead it returns a new dataframe. If we want to update the existing DataFrame in place then we need to pass another attribute i.e.

inplace=True

Let’s understand by examples,

Let’s create a DataFrame object containing student details i.e.

# List of Tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]


#Create a DataFrame object
dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 

Delete a single Row in DataFrame by Row Index Label

Contents of DataFrame object dfObj is,

Original DataFrame pointed by dfObj

Let’s delete the row with index ‘d’ from DataFrame dfObj i.e.

# Delete row with index label 'b' 
modDfObj = dfObj.drop('b')

Contents of returned dataframe object modDfObj will be,

Row with index label ‘b’ is not in new DataFrame Object. As default value of inPlace is false, so contents of dfObj will not be modified.

Delete Multiple Rows in DataFrame by Index Labels

Contents of DataFrame object dfObj is,

Original DataFrame pointed by dfObj

Let’s delete the rows with index ‘b’ , ‘c’ & ‘e’ from above dataframe i.e.

# Delete rows with index label a & b    
modDfObj = dfObj.drop(['a' , 'b'])

Contents of returned dataframe object modDfObj will be,

DataFrame without rows a & b

As default value of inPlace is false, so contents of dfObj will not be modified.

Delete a Multiple Rows by Index Position in DataFrame

Contents of dataframe object dfObj is,

Original DataFrame pointed by dfObj

As df.drop() function accepts only list of index label names only, so to delete the rows by position we need to create a list of index names from positions and then pass it to drop().
Suppose we want to delete the first two rows i.e. rows at index position 0 & 1 from the above dataframe object. Let’s see how to do that,

# Delete row at index position 0 & 1
modDfObj = dfObj.drop([dfObj.index[0] , dfObj.index[1]])

Contents of returned dataframe object modDfObj will be,

DataFrame without rows a & b

As default value of inPlace is false, so contents of dfObj will not be modified.

Delete rows from dataFrame in Place

In all the above examples drop() function was not updating the existing dataframe object, it was returning a new dataframe object.
So, to update the existing dataframe object we need to pass the parameter inPlace with value True. Let’s understand by example,

Contents of original dataframe object dfObj is,

Original DataFrame pointed by dfObj

Drop a column ‘a’ & ‘b’ from dfObj in place i.e.

# Drop Colums a & b from dfObj in place
dfObj.drop(['a' , 'b'], inplace=True)

Contents of updated dfObj is,

DataFrame without rows a & b

Complete example is as follows,

import pandas as pd

def main():
    
    # List of Tuples
    students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
                 ('Riti', 30, 'Delhi' , 'India' ) ,
                 ('Vikas', 31, 'Mumbai' , 'India' ) ,
                 ('Neelu', 32, 'Bangalore' , 'India' ) ,
                 ('John', 16, 'New York' , 'US') ,
                 ('Mike', 17, 'las vegas' , 'US')  ]
    
    
    #Create a DataFrame object
    dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 
    
    print("Original Dataframe" , dfObj, sep='\n')
    
    print("**** Delete a single row by index label ****")
   
    # Delete row with index label 'b' 
    modDfObj = dfObj.drop('b')
    
    print("New Dataframe" , modDfObj, sep='\n')
    
    print("**** Delete multiple rows by label names ****")

    # Delete rows with index label a & b    
    modDfObj = dfObj.drop(['a' , 'b'])
    
    print("**** Delete multiple rows by Index Position ****")
    
    # Delete row at index position 0 & 1
    modDfObj = dfObj.drop([dfObj.index[0] , dfObj.index[1]])
    
    print("New Dataframe with Deleted Rows at Index position 0 and 1" , modDfObj, sep='\n')
      
    print("**** Delete multiple rows from dataFrame in Place")
    
    print("Original Dataframe" , dfObj, sep='\n')
    
    # Drop Colums a & b from dfObj in place
    dfObj.drop(['a' , 'b'], inplace=True)
    
    print("Updated  Dataframe dfObj" , dfObj, sep='\n')
    
    
    
if __name__ == '__main__':
    main()

Output:
Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
**** Delete a single row by index label ****
New Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
**** Delete multiple rows by label names ****
**** Delete multiple rows by Index Position ****
New Dataframe with Deleted Rows at Index position 0 and 1
    Name  Age       City Country
c  Vikas   31     Mumbai   India
d  Neelu   32  Bangalore   India
e   John   16   New York      US
f   Mike   17  las vegas      US
**** Delete multiple rows from dataFrame in Place
Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
Updated  Dataframe dfObj
    Name  Age       City Country
c  Vikas   31     Mumbai   India
d  Neelu   32  Bangalore   India
e   John   16   New York      US
f   Mike   17  las vegas      US

 

Join a list of 2000+ Programmers for latest Tips & Tutorials