Table of Contents
To filter a Pandas dataframe by a column that is not equal to specific values, you can use the “!=” operator. This will allow you to specify the values that you do not want included in your filtered dataframe. By using this operator, you can easily remove unwanted data and focus on the specific values that are of interest to you. Additionally, you can combine this operator with other filters and conditions to create more complex and precise data filters. Overall, using the “!=” operator can help efficiently manipulate and analyze data in a Pandas dataframe.
Pandas: Filter by Column Not Equal to Specific Values
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 DataFrameprint(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.
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I filter a Pandas dataframe by a column that is not equal to specific values?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-by-a-column-that-is-not-equal-to-specific-values/
stats writer. "How can I filter a Pandas dataframe by a column that is not equal to specific values?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-by-a-column-that-is-not-equal-to-specific-values/.
stats writer. "How can I filter a Pandas dataframe by a column that is not equal to specific values?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-by-a-column-that-is-not-equal-to-specific-values/.
stats writer (2024) 'How can I filter a Pandas dataframe by a column that is not equal to specific values?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-filter-a-pandas-dataframe-by-a-column-that-is-not-equal-to-specific-values/.
[1] stats writer, "How can I filter a Pandas dataframe by a column that is not equal to specific values?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I filter a Pandas dataframe by a column that is not equal to specific values?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
