How can I drop rows in a Pandas dataframe that contain a specific value?

How can I drop rows in a Pandas dataframe that contain a specific value?

The process of dropping rows in a Pandas dataframe that contain a specific value involves using the “drop” function in Pandas and specifying the value to be dropped in the parameters. This will remove all rows that contain the specified value, thus allowing for the modification of the dataframe according to the desired criteria. This can be a useful tool for data cleaning and organizing in data analysis tasks.

Pandas: 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

Cite this article

stats writer (2024). How can I drop rows in a Pandas dataframe that contain a specific value?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-drop-rows-in-a-pandas-dataframe-that-contain-a-specific-value/

stats writer. "How can I drop rows in a Pandas dataframe that contain a specific value?." PSYCHOLOGICAL SCALES, 30 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-drop-rows-in-a-pandas-dataframe-that-contain-a-specific-value/.

stats writer. "How can I drop rows in a Pandas dataframe that contain a specific value?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-drop-rows-in-a-pandas-dataframe-that-contain-a-specific-value/.

stats writer (2024) 'How can I drop rows in a Pandas dataframe that contain a specific value?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-drop-rows-in-a-pandas-dataframe-that-contain-a-specific-value/.

[1] stats writer, "How can I drop rows in a Pandas dataframe that contain a specific value?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.

stats writer. How can I drop rows in a Pandas dataframe that contain a specific value?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top