Drop a list of rows from a Pandas DataFrame

Let us see how to drop a list of rows in a Pandas DataFrame. We can do this using the drop() function. We will also pass inplace = True as it makes sure that the changes we make in the instance are stored in that instance without doing any assignment
Over here is the code implementation of how to drop list of rows from the table :

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# imnport the module
import pandas as pd
   
# creating a DataFrame
dictionary = {'Names':['Simon', 'Josh', 'Amen', 'Habby'
                       'Jonathan', 'Nick'], 
              'Countries':['AUSTRIA', 'BELGIUM', 'BRAZIL'
                           'FRANCE', 'INDIA', 'GERMANY']}
table = pd.DataFrame(dictionary, columns = ['Names', 'Countries'], 
                     index = ['a', 'b', 'c', 'd', 'e', 'f'])
   
display(table)
   
# gives the table with the dropped rows
display("Table with the dropped rows")
display(table.drop(['a', 'd']))
   
# gives the table with the dropped rows 
# shows the reduced table for the given command only
display("Reduced table for the given command only")
display(table.drop(table.index[[1, 3]]))
   
# it gives none but it makes changes in the table 
display(table.drop(['a', 'd'], inplace = True))
   
# final table
print("Final Table")
display(table)
   
# table after removing range of rows from 0 to 2(not included)
table.drop(table.index[:2], inplace = True)
   
display(table)

chevron_right


Output :

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# creating a DataFrame    
data = {'Name' : ['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Age' : [27, 24, 22, 32],
        'Address' : ['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
        'Qualification' : ['Msc', 'MA', 'MCA', 'Phd']}
table = pd.DataFrame(data)
  
# original DataFrame
display("Original DataFrame")
display(table)
  
# drop 2nd row
display("Dropped 2nd row")
display(table.drop(1))

chevron_right


Outout :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.