15.1 C
London
Friday, July 5, 2024
HomePandas in PythonDataFrame Functions in PythonPandas: How to Drop Rows that Contain a Specific Value

Pandas: How to Drop Rows that Contain a Specific Value

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories