Drop rows that contain specific values in multiple columns

We can drop specific values from multiple columns by using relational operators.

Syntax: dataframe[(dataframe.column_name operator value ) relational_operator (dataframe.column_name operator value )]

where

  • dataframe is the input dataframe
  • column_name is the column
  • operator is the relational operator

Python3




# import pandas module
import pandas as pd
 
# create dataframe with 4 columns
data = pd.DataFrame({
 
    "name": ['sravan', 'jyothika', 'harsha', 'ramya',
             'sravan', 'jyothika', 'harsha', 'ramya',
             'sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'java', 'java', 'python',
                 'python', 'python', 'html/php',
                 'html/php', 'html/php', 'php/js',
                 'php/js', 'php/js'],
    "marks": [98, 79, 89, 97, 82, 98, 90,
              87, 78, 89, 93, 94]
})
 
# drop specific values
# where marks is 98 and name is sravan
print(data[(data.marks != 98) & (data.name != 'sravan')])
 
print("------------------")
 
# drop specific values
# where marks is 98 or name is sravan
print(data[(data.marks != 98) | (data.name != 'sravan')])


Output:



How to Drop Rows that Contain a Specific Value in Pandas?

In this article, we will discuss how to drop rows that contain a specific value in Pandas. Dropping rows means removing values from the dataframe we can drop the specific value by using conditional or relational operators.

Similar Reads

Method 1: Drop the specific value by using Operators

We can use the column_name function along with the operator to drop the specific value....

Method 2: Drop Rows that Contain Values in a List

...

Method 3: Drop rows that contain specific values in multiple columns

By using this method we can drop multiple values present in the list, we are using isin() operator. This operator is used to check whether the given value is present in the list or not...

Contact Us