You can use the following methods to filter a pandas DataFrame where a column is not equal to specific values:
Method 1: Filter where Column is Not Equal to One Specific Value
#filter rows where team column is not equal to 'Nets' df_filtered = df[df['team'] != 'Nets']
Method 2: Filter where Column is Not Equal to Several Specific Values
#filter rows where team column is not equal to 'Nets', 'Mavs' or 'Kings' df_filtered = df[~df['team'].isin(['Nets', 'Mavs', 'Kings'])]
The following examples show how to use each method in practice with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['Mavs', 'Mavs', 'Nets', 'Nets', 'Heat', 'Heat', 'Kings'], 'points': [22, 28, 35, 34, 29, 28, 23]}) #view DataFrame print(df) team points 0 Mavs 22 1 Mavs 28 2 Nets 35 3 Nets 34 4 Heat 29 5 Heat 28 6 Kings 23
Example 1: Filter where Column is Not Equal to One Specific Value
We can use the following syntax to filter the DataFrame to only contain rows where the team column is not equal to ‘Nets’:
#filter rows where team column is not equal to 'Nets' df_filtered = df[df['team'] != 'Nets'] #view filtered DataFrame print(df_filtered) team points 0 Mavs 22 1 Mavs 28 4 Heat 29 5 Heat 28 6 Kings 23
Notice that each row where the team name was ‘Nets’ has been filtered out of the DataFrame.
Note: The symbol != represents “not equal” in pandas.
Example 2: Filter where Column is Not Equal to Several Specific Values
We can use the following syntax to filter the DataFrame to only contain rows where the team column is not equal to ‘Nets’, ‘Mavs’ or ‘Kings’:
#filter rows where team column is not equal to 'Nets', 'Mavs' or 'Kings' df_filtered = df[~df['team'].isin(['Nets', 'Mavs', 'Kings'])] #view filtered DataFrame print(df_filtered) team points 4 Heat 29 5 Heat 28
Notice that each row where the team name was ‘Nets’, ‘Mavs’ or ‘Kings’ has been filtered out of the DataFrame.
Note: The symbol ~ represents “not” in pandas.
Documentation: You can find the complete documentation for the pandas isin() function here.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Pandas: How to Add Filter to Pivot Table
Pandas: How to Filter for “Not Contains”
Pandas: How to Filter Rows that Contain a Specific String