How to Delete Row in Python Pandas DataFrame || delete row using drop function in DataFrame

 How to Delete Row in Python Pandas DataFrame

We can delete row in Python pandas DataFrame by using the “drop” function

To delete any row in DataFrame we use the label or index. By using a label we can easily delete any row from DataFrame. In our example r1, r2, r3, r4, and r5 are labels.
import pandas as pd
data= {‘Name’ : [‘Rahul’, ‘Keshu’, ‘Amit’, ‘Lakhan’, ‘Raj’], ‘Age’ : [15,14,19,16,15] }
df=pd.DataFrame(data, index=[‘r1′,’r2′,’r3′,’r4′,’r5’])
print(“before deleting DataFrame is“)
print (df)
print(“after deleting the first row with label ‘r1’ using ‘drop’ function”)
df=df.drop(‘r1’)
print (df)

its output will be

before deleting DataFrame is

Name        Age

r1           Rahul        15

r2           Keshu       14

r3           Amit         19

r4          Lakhan      16

after deleting the first row with label ‘r1’ using ‘drop’ function

Name        Age

r2           Keshu       14

r3           Amit         19

r4          Lakhan      16

If you like my post please share it with your friends by simply clicking the below social media button, and Don’t forget to comment so we will update ourselves.

Share in a Single Click

Leave a Reply

Your email address will not be published. Required fields are marked *