How to Delete a Row Based on a Column Value in a Pandas DataFrame


Let’s see how we can delete a row based on a column value in a Pandas DataFrame.

1. Delete rows based on single column value

Suppose we have a DataFrame df, and we want to delete a row if the value of the column col in that row is equal to 0.

We have many different ways to drop a row based on a value. In this example, we’ll be dropped rows whose col value is 0.

df = df[df.col != 0]
df = df[df.col.ne(0)]
df = df[~df.col.eq(0)]
df = df.query('col != 0')
df = df.drop(df[df['col'] == 0].index)
df.drop(df.loc[df['col'] == 0].index, inplace=True)
df.drop(df.index[df['col'] == 0], inplace = True)

We can also drop rows based on multiple values for a single column.

Here, we’ll drop a row if col is equal to 0, 1, or 2.

df = df[~df['col'].isin([0, 1, 2])]

2. Delete rows based on multiple columns' values

Let’s say our df DataFrame now contains col1 and col2, and we want to delete a row if col1 == 0 and col2 == 999.

df = df[(df.col1 != 0) & (df.col2 != 999)]