Pandas Coding Interview Checklist: Groupby, Joins, and Clean Answers
Many data analyst, data scientist, and analytics engineering interviews include a live pandas round. You get a DataFrame or two, a business question, and 20 to 40 minutes to produce an answer while explaining your thinking. It looks easy until you blank on whether it's merge or join, or your groupby returns a shape you didn't expect.
This pandas coding interview checklist covers the operations that come up most, the common traps, and a simple way to talk through your work.
Before you write any code
- [ ] Look at the data first. Say you'd run
df.head(),df.dtypes, anddf.shape. Interviewers like seeing you check before you assume. - [ ] Confirm the grain. One row per user? Per order? Per event? Most wrong answers come from misunderstanding the grain.
- [ ] Ask about missing values and duplicates. "Can a user appear twice? Are there nulls in the amount column?"
- [ ] Restate the question as an output shape. "So I need one row per country with total revenue, sorted descending."
Core operations you should write without thinking
- [ ] Selecting columns:
df[["a", "b"]] - [ ] Filtering rows:
df[df["amount"] > 0]and combining conditions with&and|inside parentheses - [ ] Creating columns:
df["total"] = df["price"] * df["qty"] - [ ] Sorting:
df.sort_values("total", ascending=False) - [ ] Renaming:
df.rename(columns={"old": "new"}) - [ ] Dates:
pd.to_datetime(df["ts"]), then.dt.date,.dt.month, or.dt.to_period("M")
A common trap: forgetting parentheses around each condition. df[df.a > 1 & df.b < 2] fails or misbehaves; write df[(df.a > 1) & (df.b < 2)].
Groupby and aggregation
- [ ] Basic aggregation:
df.groupby("country")["revenue"].sum() - [ ] Multiple aggregations with named outputs:
summary = (
df.groupby("country")
.agg(total=("revenue", "sum"), orders=("order_id", "nunique"))
.reset_index()
)
- [ ]
reset_index()when you want a regular DataFrame back - [ ]
transformto attach a group value to every row, such as each order's share of its user's total:
df["user_total"] = df.groupby("user_id")["amount"].transform("sum")
df["share"] = df["amount"] / df["user_total"]
- [ ]
nuniquevscount: unique users vs number of rows. Say which one the question needs.
Merges and joins
- [ ]
pd.merge(left, right, on="user_id", how="left") - [ ] Know when you need
leftvsinner. "Users with no orders should still appear" means left join. - [ ] Check row counts after a merge. If they jumped, the key isn't unique on one side. Saying "I'd check
len(df)before and after" shows maturity. - [ ] Different column names:
left_onandright_on - [ ] Use
indicator=Trueto find rows that didn't match, which is handy for "customers who never ordered" questions.
Missing values and duplicates
- [ ]
df.isna().sum()to count nulls per column - [ ]
fillna(0)only when zero makes sense. Say why. - [ ]
dropna(subset=["col"])to drop rows with nulls in specific columns - [ ]
drop_duplicates(subset=["user_id"], keep="first")after sorting, to keep the earliest or latest row
Window-style logic
SQL interviews ask for window functions; pandas rounds ask for the same ideas.
- [ ] Ranking within groups:
df.groupby("dept")["salary"].rank(method="dense", ascending=False) - [ ] Top N per group: sort, then
groupby(...).head(n) - [ ] Running totals:
df.groupby("user_id")["amount"].cumsum() - [ ] Previous row values:
df.groupby("user_id")["ts"].shift(1)for gaps between events - [ ] Rolling averages:
df["amount"].rolling(7).mean()on time-ordered data
Reshaping
- [ ]
pivot_table(index="month", columns="country", values="revenue", aggfunc="sum", fill_value=0) - [ ]
meltto go from wide to long - [ ] Know that
pivotfails on duplicate index/column pairs whilepivot_tableaggregates them
Performance talking points
You rarely need deep optimization in an interview, but one or two remarks help:
- Prefer vectorized operations over
applywith Python functions or explicit loops. applyrow by row is slow on large data; say you'd replace it if the data were big.- Categorical dtypes save memory on repeated strings.
How to explain your answer
A simple structure works well:
- State the plan: "I'll filter to completed orders, group by country, sum revenue, then sort."
- Write it in small steps with intermediate variables instead of one giant chain, at least at first.
- Check the result shape and a sample of rows.
- Mention assumptions: "I'm treating null amounts as zero because they're unpaid orders."
- Offer an alternative: "In SQL this would be a GROUP BY with a HAVING clause."
Common mistakes
- Chained indexing like
df[df.a > 0]["b"] = 1, which may not modify the original. Usedf.loc[df.a > 0, "b"] = 1. - Forgetting that
groupbydrops null keys by default. - Merging on a non-unique key and silently duplicating rows.
- Writing a perfect one-liner nobody can follow. Clarity beats cleverness.
Practice out loud
Pandas rounds test fluency and communication as much as knowledge. Luna Interview helps you practice explaining your steps calmly under time pressure, so the real round feels like a conversation you've had before.
Run through this checklist a few times with real datasets, saying each step out loud, and you'll walk in ready.
Rehearse with an AI interview copilot, then browse commonly reported interview questions.