Home >Backend Development >Python Tutorial >How Can I Use Pandas' `isin` Function to Mimic SQL's `IN` and `NOT IN` Operators?

How Can I Use Pandas' `isin` Function to Mimic SQL's `IN` and `NOT IN` Operators?

Susan Sarandon
Susan SarandonOriginal
2024-12-21 22:25:22166browse

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

  • Conciseness: The isin function provides a concise way to perform IN and NOT IN filtering, reducing the need for convoluted code.
  • Flexibility: isin can be used with any type of Series, including strings, integers, and objects.
  • Efficiency: isin utilizes optimized algorithms to perform filtering, making it efficient even for large datasets.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn