To delete DataFrame rows in Pandas based on column values, you can use the .drop() method.
Delete DataFrame rows in Pandas based on single-column value
import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30, 40, 50],
'B': [60, 70, 80, 90, 100],
'C': [110, 120, 130, 140, 150],
'D': [160, 170, 180, 190, 200],
'E': [210, 220, 230, 240, 250]})
print("Before removing a row based on a single column value")
print(df)
print("After removing a row based on a single column value")
# Delete rows where the "C" column has 140 value
df = df.drop(df[df['C'] == 140].index)
print(df)
Output
We removed the row from the DataFrame where the value in column ‘C’ is equal to 140.
Delete DataFrame rows in Pandas based on multiple column values
import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30, 40, 50],
'B': [60, 70, 80, 90, 100],
'C': [110, 120, 130, 140, 150],
'D': [160, 170, 180, 190, 200],
'E': [210, 220, 230, 240, 250]})
print("Before removing a row based on multiple column values")
print(df)
print("After removing a row based on a multiple column values")
# Delete rows based on multiple column values
df = df.drop(df[(df['B'] == 90) & (df['D'] == 190)].index)
print(df)
Output
Here, we removed the row where ‘B’ is 90 and ‘D’ is 190, the fourth row in your DataFrame whose index is 3.