Filter Pandas dataframe in Python using 'in' and 'not in' Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The in and not in operators can be used with Pandas DataFrames to check if a given value or set of values is present in the DataFrame or not using Python. The in-operator returns a boolean value indicating whether the specified value is present in the DataFrame, while the not-in-operator returns a boolean value indicating whether the specified value is not present in the DataFrame. This operator can be used with the .query() method of a Pandas DataFrame to filter the DataFrame based on a given set of values. The .query() method takes a string containing a Boolean expression as input and returns a new DataFrame containing only the rows that satisfy the given expression. Filter Pandas Dataframe in Python using 'in' keyword The in keyword has two purposes, first to check if a value is present in a list, tuple, range, string, etc. and another is to iterate through a sequence in a for a loop. Example 1 Here is an example of how the in operator can be used with the .query() method to filter a DataFrame: Python3 import pandas as pd # Create a DataFrame with some sample data df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]}) # Filter the DataFrame to include only rows # where column "A" has a value of 1 or 2 df = df.query("A in [1, 2]") # Print the resulting DataFrame print(df) Output: A B 0 1 5 1 2 6 Example 2 The in operator can also be used in more complex expressions with the .query() method, to combine multiple conditions and apply logical operators such as and/or. Python3 # Filter the DataFrame to include only rows # where column "A" has a value of 1 or 2, and # column "B" has a value of 6 or 7 df = df.query("A in [1, 2] and B in [6, 7]") # Print the resulting DataFrame print(df) Output: A B 1 2 6Filter Pandas Dataframe in Python using the 'not in' keyword Python not keyword is a logical operator which is usually used for figuring out the negation or opposite boolean value of the operand. Example 1 To use the `not in` operator with the .query() method of a Pandas DataFrame, you can simply negate the expression using the not keyword. Python3 import pandas as pd # Create a DataFrame with some sample data df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]}) # Filter the DataFrame to exclude rows # where column "A" has a value of 1 or 2 df = df.query("not A in [1, 2]") # Print the resulting DataFrame print(df) Output: A B 2 3 7 3 4 8 Example 2 Here is the example with the 'not in' operator. Python3 # Filter the DataFrame to exclude rows # where column "A" has a value of 1 or 2, and # column "B" has a value of 6 or 7 df = df.query("not (A in [1, 2] and B in [6, 7])") # Print the resulting DataFrame print(df) Output: A B 0 1 5 2 3 7 3 4 8 Comment More infoAdvertise with us Next Article Filter Pandas dataframe in Python using 'in' and 'not in' S sumeshk333 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2022 Practice Tags : python Similar Reads Find location of an element in Pandas dataframe in Python In this article, we will see how to find the position of an element in the dataframe using a user-defined function. Let's first Create a simple dataframe with a dictionary of lists. How to find an element in Pandas Dataframe?In a Pandas DataFrame, you can find the position of a specific element or c 3 min read Python | Pandas dataframe.filter() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.filter() function is used to Subset rows or columns of dataframe acco 2 min read Check if dataframe contains infinity in Python - Pandas When working with data, you may encounter infinity values (positive or negative), represented as np.inf and -np.inf in Python using the NumPy library. It's important to check if your Pandas DataFrame contains any infinity values before proceeding with analysis. Let's explore different methods to det 3 min read How to Use âNOT INâ Filter in Pandas? The "NOT IN"(â¼) filter is a membership operator used to check whether the data is present in DataFrame or not. Pandas library does not have the direct NOT IN filter in Python, but we can perform the NOT IN filter by negating the isin() operator of Pandas. In this tutorial, we will provide a step-by 3 min read Check if a value exists in a DataFrame using in & not in operator in Python-Pandas In this article, Letâs discuss how to check if a given value exists in the dataframe or not.Method 1 : Use in operator to check if an element exists in dataframe.  Python3 # import pandas library import pandas as pd # dictionary with list object in values details = { 'Name' : ['Ankit', 'Aishwarya', 3 min read Filter Pandas DataFrame Based on Index In this article, we are going to see how to filter Pandas Dataframe based on index. We can filter Dataframe based on indexes with the help of filter(). This method is used to Subset rows or columns of the Dataframe according to labels in the specified index. We can use the below syntax to filter Dat 3 min read How to Filter Using 'in' and 'not in' Like in SQL in Polars Python Polars, a fast and versatile DataFrame library in Rust with bindings for Python, offers efficient data manipulation capabilities. For those transitioning from SQL or those who need to perform complex data manipulations, Polars provides a familiar and powerful set of tools that mimic SQL-like 3 min read Python | Pandas DataFrame.isin() In this article, we will explore the Pandas DataFrame.isin() method provided by the Pandas library in Python. Python is widely recognized for its proficiency in data analysis, largely attributed to its exceptional ecosystem of data-centric packages. Among these, Pandas stands out as an essential too 2 min read Python | Pandas Series/Dataframe.any() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas any() method is applicable both on Series and Dataframe. It checks whether any 3 min read Like