Pandas
Dataframe
Data Selection
Python
Data Analysis

Use a list of values to select rows from a Pandas dataframe

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Filtering a Pandas DataFrame by a list of allowed values is a routine task in reporting, data cleaning, and exploratory analysis. The usual tool is Series.isin, which turns membership testing into a boolean mask that you can apply to rows. Once you understand that pattern, it scales to multiple columns and more complex filters.

Use isin for Membership-Based Row Selection

The simplest case is "keep rows whose value in one column appears in this list."

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "employee_id": [101, 102, 103, 104, 105],
6        "name": ["Alice", "Bob", "Cara", "Dan", "Eli"],
7        "department": ["HR", "IT", "Finance", "IT", "HR"],
8        "salary": [65000, 82000, 91000, 79000, 67000],
9    }
10)
11
12allowed_departments = ["HR", "IT"]
13
14filtered = df[df["department"].isin(allowed_departments)]
15print(filtered)

df["department"].isin(allowed_departments) produces a boolean Series. Pandas then keeps only the rows where that Series is True.

This is both readable and efficient for most normal workloads.

Negate the Condition When You Need Exclusion

Often the real requirement is "drop rows with values from this list." Use ~ to negate the boolean mask.

python
excluded_departments = ["Finance"]
remaining = df[~df["department"].isin(excluded_departments)]
print(remaining)

That pattern is easier to read than building a long chain of != checks.

Combine Membership Checks with Other Conditions

Membership filtering becomes more useful when you combine it with comparisons on other columns. In Pandas, use & for logical AND and | for logical OR, and wrap each condition in parentheses.

python
1high_paid_it_or_hr = df[
2    (df["department"].isin(["HR", "IT"])) & (df["salary"] >= 70000)
3]
4
5print(high_paid_it_or_hr)

This works because both sides of & are boolean Series aligned to the same index.

Filter with Values from Another Collection

The list does not need to be hard-coded. It can come from another DataFrame, a file, or user input.

python
priority_ids = pd.Series([102, 105, 999])
priority_rows = df[df["employee_id"].isin(priority_ids)]
print(priority_rows)

Pandas accepts many iterable types here, including Python lists, sets, tuples, NumPy arrays, and Series objects. A set can be useful when the source values are already unique and you want clear intent.

Use query When You Prefer Expression Syntax

Some teams prefer query for human-readable filters, especially in notebooks. It can express membership checks too.

python
allowed_departments = ["HR", "IT"]
filtered = df.query("department in @allowed_departments")
print(filtered)

query is convenient, but isin remains the more direct option when membership is the main operation. isin also avoids expression-string parsing, which some developers prefer for maintainability.

Selecting by Several Columns

Sometimes the list applies to one column, but another column determines which rows are actually useful. You can project only the columns you need after filtering.

python
1result = df.loc[
2    df["department"].isin(["HR", "IT"]),
3    ["employee_id", "name", "department"],
4]
5
6print(result)

Using .loc makes the selection explicit: first rows, then columns.

Handling Missing Values

Missing values deserve special attention. isin checks whether each value appears in the given collection, but NaN behavior can be surprising if you expect it to match like a normal string or number.

python
1df_with_missing = pd.DataFrame(
2    {
3        "department": ["HR", None, "IT", "Finance"],
4        "name": ["Alice", "Bob", "Cara", "Dan"],
5    }
6)
7
8mask = df_with_missing["department"].isin(["HR", "IT"])
9print(mask)

If your business rule needs missing values included, combine isin with isna.

python
mask = df_with_missing["department"].isin(["HR", "IT"]) | df_with_missing["department"].isna()
print(df_with_missing[mask])

Common Pitfalls

The most common mistake is writing df["department"] in ["HR", "IT"]. That checks whether the entire Series object is in the list, not whether each row value is in the list.

Another frequent issue is using Python and or or instead of & or |. With Pandas Series, and and or do not perform element-wise boolean logic.

It is also easy to forget parentheses around each condition when combining filters. Without them, operator precedence can produce confusing errors.

Finally, remember that string matching is exact. "hr" does not match "HR" unless you normalize case first.

Summary

  • Use Series.isin to filter rows where a column matches any value from a list.
  • Negate the mask with ~ when you want exclusion instead of inclusion.
  • Combine membership checks with other comparisons using & and |.
  • Use .loc when you want to filter rows and choose output columns in one step.
  • Handle missing values and case normalization explicitly when your rules require them.

Course illustration
Course illustration

All Rights Reserved.