Pandas: How to Drop Rows that Contain a Specific Value


You can use the following syntax to drop rows in a pandas DataFrame that contain a specific value in a certain column:

#drop rows that contain specific 'value' in 'column_name'
df = df[df.column_name != value]

You can use the following syntax to drop rows in a pandas DataFrame that contain any value in a certain list:

#define values
values = [value1, value2, value3, ...]

#drop rows that contain any value in the list
df = df[df.column_name.isin(values) == False]

The following examples show how to use this syntax in practice.

Example 1: Drop Rows that Contain a Specific Value

The following code shows how to drop any rows that contain a specific value in one column:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'name': ['Dirk', 'Kobe', 'Tim', 'Lebron'],
                   'rebounds': [11, 7, 14, 7],
                   'points': [26, 31, 22, 29]})

#view DataFrame
df

        team	name	rebounds points
0	Mavs	Dirk	11	 26
1	Lakers	Kobe	7	 31
2	Spurs	Tim	14	 22
3	Cavs	Lebron	7	 29

#drop any rows that have 7 in the rebounds column
df = df[df.rebounds != 7]

#view resulting DataFrame
df

        team	name	rebounds points
0	Mavs	Dirk	11	 26
2	Spurs	Tim	14	 22

Example 2: Drop Rows that Contain Values in a List

The following code shows how to drop any rows in the DataFrame that contain any value in a list:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'name': ['Dirk', 'Kobe', 'Tim', 'Lebron'],
                   'rebounds': [11, 7, 14, 7],
                   'points': [26, 31, 22, 29]})

#view DataFrame
df

        team	name	rebounds points
0	Mavs	Dirk	11	 26
1	Lakers	Kobe	7	 31
2	Spurs	Tim	14	 22
3	Cavs	Lebron	7	 29

#define list of values
values = [7, 11]

#drop any rows that have 7 or 11 in the rebounds column
df = df[df.rebounds.isin(values) == False]

#view resulting DataFrame
df

        team	name	rebounds points
2	Spurs	Tim	14	 22

Example 3: Drop Rows that Contain Specific Values in Multiple Columns

The following code shows how to drop any rows in the DataFrame that contain a specific value in one of several columns:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Lakers', 'Spurs', 'Cavs'],
                   'name': ['Dirk', 'Kobe', 'Tim', 'Lebron'],
                   'rebounds': [11, 7, 14, 7],
                   'points': [26, 31, 22, 29]})

#view DataFrame
df

        team	name	rebounds points
0	Mavs	Dirk	11	 26
1	Lakers	Kobe	7	 31
2	Spurs	Tim	14	 22
3	Cavs	Lebron	7	 29

#drop any rows that have 11 in the rebounds column or 31 in the points column
df = df[(df.rebounds != 11) & (df.points != 31)]

#view resulting DataFrame
df

team	name	rebounds	points
2	Spurs	Tim	14	22
3	Cavs	Lebron	7	29

Additional Resources

How to Drop Rows by Index in Pandas
How to Drop Columns by Index in Pandas
How to Drop Rows that Contain a Specific String in Pandas

Leave a Reply

Your email address will not be published.