How to Delete Row in Python Pandas DataFrame
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’])
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)
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