🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.isin()

AI & DATA SCIENCE // df-isin

df.isin() checks each element against a given list (or set, or dict) of values, returning a boolean DataFrame or Series indicating which elements are members of that collection.

Syntax

df.isin(values)

Deep Dive Course

isin() is the vectorized equivalent of checking membership in a list for every element at once — calling it on a column with a list of allowed values returns a boolean Series marking which rows have a value matching any of them, which you then typically use directly for boolean indexing. Passing a dict instead lets you specify different allowed values per column, checking each column against its own specific list rather than the same list for every column.

1Understanding df.isin()

isin() is the vectorized equivalent of checking membership in a list for every element at once — calling it on a column with a list of allowed values returns a boolean Series marking which rows have a value matching any of them, which you then typically use directly for boolean indexing. Passing a dict instead lets you specify different allowed values per column, checking each column against its own specific list rather than the same list for every column.

💡

Use series.isin([...]) as the vectorized alternative to writing a series of | (OR) comparisons for checking membership against several possible values — it's both more concise and more efficient than chaining several == comparisons with |.

editor.html
import pandas as pd

df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
print(df["status"].isin(["pending", "active"]))
localhost:3000

2Practical Example

Here is a real-world application of df.isin() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
filtered = df[~df["status"].isin(["cancelled"])]
print(filtered)
localhost:3000

3Best Practices

Follow these guidelines when working with df.isin():

1. Use isin() instead of chaining multiple == comparisons with | when checking a column against several possible values

2. Combine isin() with the negation operator to select rows that do not match any value in a list

3. Pass a dict to isin() when different columns need to be checked against different allowed-value lists, rather than calling isin() separately per column

⚠️

Tip: Use series.isin([...]) as the vectorized alternative to writing a series of | (OR) comparisons for checking membership against several possible values — it's both more concise and more efficient than chaining several == comparisons with |.

editor.html
import pandas as pd

df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
print(df["status"].isin(["pending", "active"]))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
print(df["status"].isin(["pending", "active"]))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
filtered = df[~df["status"].isin(["cancelled"])]
print(filtered)

Best Practices

  • Use isin() instead of chaining multiple == comparisons with | when checking a column against several possible values
  • Combine isin() with the negation operator to select rows that do not match any value in a list
  • Pass a dict to isin() when different columns need to be checked against different allowed-value lists, rather than calling isin() separately per column

Interview Question

How would you select every row except those matching one of several specific values, using isin()?

Hint: Think about how to invert a boolean Series.

Call isin() with the list of values you want to exclude, which produces a boolean Series marking True for every row that matches one of them, then negate that entire Series with the ~ operator before using it for boolean indexing. The negation flips every True to False and vice versa, so the resulting selection keeps exactly the rows that did not match any value in the excluded list.

Exercises

MediumPractice using df.isin() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
print(df["status"].isin(["pending", "active"]))

Frequently Asked Questions

How would you select every row except those matching one of several specific values, using isin()?

Call isin() with the list of values you want to exclude, which produces a boolean Series marking True for every row that matches one of them, then negate that entire Series with the ~ operator before using it for boolean indexing. The negation flips every True to False and vice versa, so the resulting selection keeps exactly the rows that did not match any value in the excluded list.

Related Functions

boolean-indexingdf-wheresets