Home >Backend Development >Python Tutorial >How Can I Use Pandas' `isin` Function to Mimic SQL's `IN` and `NOT IN` Operators?
Query Pandas DataFrames with the Power of SQL's IN and NOT IN
Filtering data in Pandas DataFrames is a common task in data processing. Pandas provides various methods to achieve this, including the intuitive isin function. This article demonstrates how you can leverage isin to filter data, mimicking SQL's IN and NOT IN operators.
Understanding the Problem
SQL's IN and NOT IN operators allow you to filter data based on whether a value is contained within or excluded from a specified list. This functionality is essential for isolating specific records or removing unwanted data.
Using isin to Filter Data
Pandas offers the isin function, which operates on Series objects. It returns a boolean mask indicating whether each element in the Series matches any value in a provided list or array.
IN Filtering
To perform an IN operation, simply pass the list of values you want to match to the isin function using the following syntax:
something.isin(somewhere)
NOT IN Filtering
To perform a NOT IN operation, use the negation operator ~ before the isin function:
~something.isin(somewhere)
Worked Example
Consider the following DataFrame df and a list of countries to keep countries_to_keep:
df = pd.DataFrame({'country': ['US', 'UK', 'Germany', 'China']}) countries_to_keep = ['UK', 'China']
To find the rows where country is included in countries_to_keep:
df[df.country.isin(countries_to_keep)]
Output:
country 1 UK 3 China
To find the rows where country is not included in countries_to_keep:
df[~df.country.isin(countries_to_keep)]
Output:
country 0 US 2 Germany
Benefits of Using isin
By understanding and leveraging the isin function, you can effectively filter Pandas DataFrames based on the values in your specified lists or arrays, empowering your data processing tasks with the power of SQL's IN and NOT IN operators.
The above is the detailed content of How Can I Use Pandas' `isin` Function to Mimic SQL's `IN` and `NOT IN` Operators?. For more information, please follow other related articles on the PHP Chinese website!