isin
df["col"].isin(values) returns True for each row whose value is in the given list, set, Series or array, so df[df["col"].isin(["a", "b"])] keeps those rows. Put ~ in front to keep the rows that are not in the list.
Syntax
Series.isin(values) | DataFrame.isin(values)| Part | What it is |
|---|---|
values | List, set, Series, array or (for DataFrame.isin) a dict mapping column to allowed values |
Examples
import pandas as pd
df = pd.DataFrame({"name": ["Ana", "Ben", "Cara", "Dan"],
"dept": ["Sales", "IT", "Sales", "HR"]})
df[df["dept"].isin(["IT", "HR"])]Result: name dept 1 Ben IT 3 Dan HR
df[~df["dept"].isin(["IT", "HR"])]Result: name dept 0 Ana Sales 2 Cara Sales
active = pd.Series(["Ana", "Dan"])
df["is_active"] = df["name"].isin(active)
dfResult: name dept is_active 0 Ana Sales True 1 Ben IT False 2 Cara Sales False 3 Dan HR True
Membership against another Series or column is a cheap way to do a semi-join.
Common mistakes
- isin returns all False because of a type mismatch: df["id"].isin(["1", "2"]) on an integer column; make the list the same type as the column.
- Passing a single string: isin("abc") raises TypeError, wrap it in a list: isin(["abc"]).
- Using == with a list (df.col == ["a", "b"]) compares element-wise and errors or gives nonsense; isin is the right tool.
Practice isin
These drills check your answer on live data, free, no account needed.
- Match a list of values with isinReturn the employees who work in Boston or Denver. Keep all columns and the original row labels.
Questions
How do I do a NOT IN filter in pandas?
Negate the mask with the tilde: df[~df["col"].isin(values)].
Can isin check multiple columns?
df.isin({"dept": ["IT"], "name": ["Ana"]}) returns a boolean DataFrame; combine with .all(axis=1) or .any(axis=1) to get a row mask.
Is isin case-sensitive?
Yes. Lowercase the column first: df["col"].str.lower().isin(["a", "b"]).
Related
- Filter rowsHow to filter rows in a pandas DataFrame by one or several conditions using boolean masks, with the & | ~ operators, parentheses, isin, between and query.
- DataFrame.queryHow df.query works in pandas: conditions as a string with and, or, not, in, variables with @, column names with spaces, and when to use boolean masks instead.
- str.containsHow to use str.contains in pandas to find rows containing text: case-insensitive matching, several keywords with regex, literal characters, and the NaN error.
- mergeHow to merge DataFrames in pandas like a SQL join: on a shared column, on different key names, left and outer joins, suffixes for clashing columns, validate.