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 |.
import pandas as pd
df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
print(df["status"].isin(["pending", "active"]))2Practical Example
Here is a real-world application of df.isin() showing how it is used in production Pandas code.
import pandas as pd
df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
filtered = df[~df["status"].isin(["cancelled"])]
print(filtered)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 |.
import pandas as pd
df = pd.DataFrame({"status": ["pending", "shipped", "active", "cancelled"]})
print(df["status"].isin(["pending", "active"]))