pandas Cheatsheet

Filtering

Use this pandas reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Comparison Operators

df[df["age"] > 30]
df[df["age"] >= 18]
df[df["age"] == 25]
df[df["age"] != 25]
df[df["name"] == "Alice"]

Combining Conditions

Use parentheses around each condition. Python and/or/not do not work with Series — use &, |, ~.

# AND
df[(df["age"] > 18) & (df["score"] >= 60)]

# OR
df[(df["city"] == "NYC") | (df["city"] == "LA")]

# NOT
df[~(df["status"] == "inactive")]

# Three or more conditions
df[(df["a"] > 0) & (df["b"] < 100) & (df["c"] != "X")]

.query() String Expressions

df.query("age > 30")
df.query("city == 'NYC'")
df.query("age > 18 and score >= 60")
df.query("city in ['NYC', 'LA']")
df.query("not (status == 'inactive')")
df.query("score.between(50, 90)")          # call Series methods

# Reference Python variables with @
cutoff = 30
cities = ["NYC", "LA"]
df.query("age > @cutoff and city in @cities")

# Column names with spaces — wrap in backticks
df.query("`first name` == 'Alice'")

.isin() — Membership Filter

# Keep rows where column value is in a list/set/Series
df[df["status"].isin(["active", "trial"])]
df[df["id"].isin(other_df["id"])]

# Exclude — negate with ~
df[~df["category"].isin(["spam", "deleted"])]

.between() — Range Filter

df[df["score"].between(60, 90)]                      # 60 <= score <= 90
df[df["score"].between(60, 90, inclusive="neither")] # 60 < score < 90
df[df["date"].between("2024-01-01", "2024-06-30")]   # works on datetime too

String Filters — str Accessor

s = df["name"]

s.str.contains("alice", case=False, na=False)
s.str.startswith("A")
s.str.endswith("son")
s.str.match(r"^\d{3}-")         # anchored regex (full match)
s.str.fullmatch(r"[A-Z][a-z]+") # entire string must match
s.str.len() > 5                 # by length

# Filter DataFrame
df[df["email"].str.contains(r"@gmail\.com", regex=True, na=False)]
df[df["name"].str.startswith("J")]

Null / Missing Filters

df[df["col"].isna()]           # rows where col is NaN/None/NaT
df[df["col"].notna()]          # rows where col is not null
df[df["col"].isnull()]         # alias for isna()
df[df["col"].notnull()]        # alias for notna()

# Keep rows where ANY column is null
df[df.isna().any(axis=1)]

# Keep rows where ALL columns are non-null
df[df.notna().all(axis=1)]

Numeric / Type Filters

# Positive / negative
df[df["value"] > 0]
df[df["value"].abs() > 10]

# Integer check
df[df["n"] % 2 == 0]   # even numbers

# Infinity
import numpy as np
df[np.isinf(df["val"])]
df[~np.isinf(df["val"])]

# Dtype-based (select columns, then filter rows)
num_cols = df.select_dtypes(include="number").columns
df[df[num_cols].gt(0).all(axis=1)]  # all numeric cols > 0

Duplicate Filters

df[df.duplicated()]                          # duplicate rows (keep first occurrence)
df[df.duplicated(keep=False)]               # all copies of duplicated rows
df[df.duplicated(subset=["col1", "col2"])]  # duplicates on specific columns

df[~df.duplicated()]        # keep first occurrence of each unique row
df.drop_duplicates()        # equivalent, returns deduplicated DataFrame

Top / Bottom N — nlargest / nsmallest

df.nlargest(10, "score")               # top 10 by score
df.nlargest(5, ["score", "age"])       # sort by multiple cols
df.nsmallest(10, "price")
df["score"].nlargest(5)                # works on Series too

Filtering Inside Groups

# Keep groups where size > threshold
df.groupby("dept").filter(lambda g: len(g) > 5)

# Keep groups where mean > threshold
df.groupby("category").filter(lambda g: g["sales"].mean() > 1000)

.where() / .mask() — Conditional Replacement vs Filter

# Replace non-matching with NaN (keeps DataFrame shape)
df["score"].where(df["score"] >= 60)         # <60 → NaN
df["score"].where(df["score"] >= 60, other=0) # <60 → 0

# mask() is the inverse
df["score"].mask(df["score"] < 60, other=0)  # <60 → 0

eval() for Complex Expressions

# Computed filter without intermediate Series
df[df.eval("a + b > c")]
df.query("a + b > c")      # same via query