Pandas
Dataframe
Python
SQL
Data Filtering

How to filter Pandas dataframe using 'in' and 'not in' like in SQL

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Filtering data in a Pandas DataFrame is a common operation during data analysis tasks. Pandas, being a powerful tool, supports SQL-like querying, especially when it comes to filtering data using conditions such as in and not in. This article will guide you through using these conditions effectively within Pandas.

SQL IN and NOT IN in Pandas

The SQL IN is used to filter records where a field value matches any value within a list. Similarly, NOT IN is used to exclude records where a field value matches any value within a list. In Pandas, equivalent functionality can be achieved using the isin() method and boolean indexing.

Using isin() for SQL IN Like Filtering

The isin() method on a Pandas Series allows you to filter DataFrame rows where the column's value is present in a predefined list of values. Here’s how you can use it:

Example:

Suppose we have the following DataFrame:

python
1import pandas as pd
2
3data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
4        'Age': [25, 30, 35, 40]}
5df = pd.DataFrame(data)

If you want to filter the rows where the name is either 'Alice' or 'Bob', you can use isin() like this:

python
filtered_df = df[df['Name'].isin(['Alice', 'Bob'])]
print(filtered_df)

This will only include the rows where the 'Name' column matches 'Alice' or 'Bob'.

Inverting isin() for SQL NOT IN Like Filtering

To implement SQL’s NOT IN, you invert the boolean series returned by isin() using the ~ operator.

Example:

Continuing from the previous DataFrame, if you want to exclude 'Alice' and 'Bob':

python
filtered_df = df[~df['Name'].isin(['Alice', 'Bob'])]
print(filtered_df)

This will exclude rows where the 'Name' column is either 'Alice' or 'Bob', thus including only 'Charlie' and 'David'.

Performance and Usage Tips

While using isin() is straightforward and powerful, there are certain scenarios where you might need to consider performance:

  • Large DataFrames: isin() can be slower on very large DataFrames. In such cases, converting the list to a set before passing it to isin() can sometimes improve performance since set lookups are generally faster than list.
  • Multiple Conditions: For multiple column filtering with in and not in conditions, ensure to carefully manage Boolean series using & (AND) and | (OR) with proper parentheses to avoid ambiguity in conditions.

Summary Table

MethodSQL EquivalentDescriptionExample
isin()INFilters rows where the column's values are in a provided list.df[df['Name'].isin(['Alice', 'Bob'])]
~ with isin()NOT INFilters rows where the column's values are not in a provided list.df[~df['Name'].isin(['Alice', 'Bob'])]

Additional Points

  • Combining Filters: These filters can be combined with other DataFrame operations to perform complex queries, such as sorting and grouping, after filtering.
  • Alternative Methods: Alternatives like query methods or boolean indexing using np.where can also be used depending primarily on readability preferences and specific use cases.

Understanding and efficiently implementing in and not in filters in Pandas parallel to their SQL counterparts, can greatly enhance your data manipulation and preprocessing capabilities in Python, leading to more robust and readable code.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design