Pandasdrill

The pandas round of a data analyst interview

In a pandas round you get one or two small tables and a business question, such as revenue by country or which customers came back. Nobody tells you to use groupby or merge. What is graded is whether the number is right, and the number is usually wrong for one of five reasons.

The five traps

The examples use two small tables: orders (order_id, customer, date, amount, status) with 8 rows, one of them cancelled, and customers (customer, country, segment) with 5 rows, one of whom never ordered.

1. Rows that should not count

Cancelled orders, test accounts and refunds sit in the same table with real amounts. Nothing fails if you include them; the total is just wrong. Read the status column before you add anything up.

print("all orders:", orders["amount"].sum())
print("completed only:", orders.loc[orders["status"] == "completed", "amount"].sum())

Result:

all orders: 1465.0
completed only: 1420.0

Write it yourself: Revenue by country, checked on two datasets.

2. A join that changes the row count

An inner merge drops rows with no match and a repeated key duplicates rows. Check the number of rows before and after every merge.

print(len(lines), "order lines;", len(lines.merge(pricelist, on="sku")), "after an inner merge;", len(lines.merge(dup_prices, on="sku", how="left")), "after a left merge on a table with a repeated key")

Result:

4 order lines; 3 after an inner merge; 5 after a left merge on a table with a repeated key

Write it yourself: A join that inflates the total, checked on two datasets.

3. The wrong denominator

A rate needs the right total underneath it. The share of customers who ordered more than once is counted over all customers who ordered, not only over those who came back.

n = orders.groupby("customer").size()
print("repeat customers:", (n >= 2).sum(), "of", len(n), "=", round((n >= 2).mean(), 2))

Result:

repeat customers: 3 of 4 = 0.75

Write it yourself: Second order within 30 days, checked on two datasets.

4. Steps in the wrong order

Shares must be computed before the table is cut to the top rows, and averages over orders are not the same as averages of per-customer averages.

print("average order:", round(orders["amount"].mean(), 2))
print("average of customer averages:", round(orders.groupby("customer")["amount"].mean().mean(), 2))

Result:

average order: 183.12
average of customer averages: 198.54

Write it yourself: Average order value by segment, checked on two datasets.

5. Counting someone twice

A customer with two orders in a month is one active customer, not two. Count distinct values, or drop duplicates on the pair you are counting.

m = orders.assign(month=orders["date"].dt.strftime("%Y-%m"))
pd.DataFrame({"orders": m.groupby("month").size(), "active_customers": m.groupby("month")["customer"].nunique()})

Result:

         orders  active_customers
month                            
2024-01       3                 2
2024-02       3                 3
2024-03       2                 2

Write it yourself: New and returning customers per month, checked on two datasets.

Try it yourself

Marketing wants a list of repeat buyers: customers with at least two completed orders. Return the columns customer and orders (the number of completed orders), most orders first, and alphabetical by customer when two have the same number.

orders: 12 rows, first 4 shown

order_idcustomerdateamountstatus
1"acme"2024-01-05120"completed"
2"birch"2024-01-12300"completed"
3"acme"2024-01-2880"completed"
4"cobalt"2024-02-0345"cancelled"

customers: 6 rows, first 4 shown

customercountrysegment
"acme""US""retail"
"birch""UK""wholesale"
"cobalt""US""retail"
"dune""DE""retail"

Free, no account needed. Real pandas runs in your browser; the first run takes a few seconds to load Python.

A seven-day practice plan

Day 1: selecting and filtering. loc, boolean masks with & and |, isin, sorting, the top rows.

Day 2: cleaning. Missing values, text to numbers, strip and lower, duplicates, dates.

Day 3: groupby. One total per group, named aggregations, two keys, transform and filter.

Day 4: merges. Inner against left, keys with different names, anti-joins, checking row counts.

Day 5: dates and windows. Month columns, pct_change, shift and diff inside a group, running totals.

Day 6: interview cases under a clock. Read the question, say which rows count, then write the code.

Day 7: redo every case you got wrong without looking at the solution.

How to talk through a case

State which rows count and which do not before you write anything. Say what one row of the answer represents: a customer, a month, an order.

After each merge, say the row count out loud. After the final step, check one number by hand against the sample table. Interviewers give credit for a wrong answer that was checked and corrected, and very little for a right-looking number that was never questioned.

Practice these live

Each exercise runs real pandas in your browser and checks your result on the sample and on a second dataset. Your first 8 are free, with no account.

Start practicing free

Related guides