All lessons

Module 9: Capstone and interviews · Lesson 9.2 · about 45 min

pandas interview questions, answered

35 pandas questions from data analyst and BI interviews and take-home tests, from loc vs iloc to merges that add rows, groupby with transform, pivots, dates and speed, each with a short answer, the line of code, and the lesson that explains it.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 9 · about 45 minutes · revision. Answer each question out loud, write the line of code, then open the answer. In a live test, say what you are doing as you type: it shows your thinking even when the syntax slips.

Interview pandas questions test a few patterns again and again: filter, group, join, rank and dates. Say the plan in words, then write the line.

In everyday terms: a driving test: the examiner asks for the same few manoeuvres. Practise those until they are automatic, and talk through what you are checking.

Basics

1. What is the difference between a Series and a DataFrame?

A Series is one column with an index; a DataFrame is a table of Series that share the same index. df['qty'] is a Series, df[['qty']] is a one-column DataFrame. (Lessons 1.3, 1.4)

2. loc or iloc?

loc selects by label (index values and column names, and slices include the end); iloc by position (0, 1, 2…, end excluded). df.loc[df.qty > 2, 'store_id'] filters and picks a column in one step. (Lesson 2.2)

3. How do you filter on two conditions?

df[(df.qty > 2) & (df.store_id == 'S01')], with brackets round each condition and &, |, ~ instead of and, or, not. Or df.query("qty > 2 and store_id == 'S01'"). (Lessons 2.3, 2.6)

4. What does inplace=True do, and should you use it?

It changes the object instead of returning a new one. Most teams avoid it: it doesn’t save memory, it breaks method chains, and it’s being phased out. Assign the result instead. (Lesson 8.1)

5. How do you take a first look at a new dataset?

shape, head(), info() (types and missing values), describe() (ranges), isna().sum(), duplicated().sum(), nunique(), and value_counts() on key text columns. (Lesson 1.6)

Cleaning

6. How do you handle missing values?

First find out why they are missing. Then drop (dropna) if few and random, fill (fillna with 0, a median, a group median via transform, or ffill for time series), or keep and flag. Say what you did in the report. (Lesson 3.1)

7. A numeric column is read as text. Why, and how do you fix it?

Something in it isn’t a number: ”$”, commas, “n/a”, spaces. Clean with .str.replace, then pd.to_numeric(col, errors='coerce') and check what turned into NaN. (Lessons 3.3, 3.4)

8. How do you find and remove duplicates?

df.duplicated().sum() for exact repeats, df.duplicated(subset=['order_id', 'product_id']) for repeated keys; drop_duplicates(subset=..., keep='last') to keep the latest. Look at a few before deleting. (Lesson 3.2)

9. Dates come in three formats. What do you do?

pd.to_datetime(col, format='mixed', dayfirst=...) or parse each format separately and combine; then check min, max and any NaT. Ambiguous ones like 03/04 need a rule from the data owner. (Lesson 3.5)

10. How do you clean messy text categories (" toronto", "Toronto ")?

.str.strip().str.title(), then value_counts() to spot the rest, and replace or map for spellings. (Lesson 3.4)

Count per group
groupby + size or value_counts.
Top N per group
sort_values + groupby().head(N), or rank.
Customers with 2+ orders
groupby(customer).order_id.nunique() ≥ 2.
Running total, month over month
cumsum, pct_change.
Physical meaning
Most questions reduce to "what is one row in the answer?" Name that grain first (one row per customer, per store-month) and the right groupby follows.

“They want the shortest code.” They want correct code you can explain, with a check: the row count, a total that still adds up, the edge case you thought of.

Watch it happen, one step at a time:

Question. What share of customers ordered more than once?
Grain of the answer. One row per customer.
Code. n = df.groupby('customer')['order_id'].nunique()
Answer. (n >= 2).mean()
customerorders2+?
C13True
C21False
C32True
C41False
→ 0.5
Say the check. nunique, not size: one order with three lines is still one order.

Grouping

11. Explain groupby in one sentence.

Split the rows into groups by a key, apply a summary to each group, and combine the results into one table. (Lesson 4.1)

12. Several summaries with names?

Named aggregation: df.groupby('store_id').agg(revenue=('revenue', 'sum'), orders=('order_id', 'nunique')). (Lesson 4.2)

13. agg vs transform vs filter?

agg returns one row per group; transform returns a value for every original row (for share of group, group means); filter keeps or drops whole groups. (Lesson 4.3)

14. Each order line's share of its store's revenue?

df['share'] = df['revenue'] / df.groupby('store_id')['revenue'].transform('sum'). (Lesson 4.3)

15. Top 3 products per store?
t = df.groupby(['store_id', 'product_id'], as_index=False)['revenue'].sum()
t.sort_values('revenue', ascending=False).groupby('store_id').head(3)

(Lessons 2.4, 4.1)

16. size() or count()?

size() counts rows including NaN; count() counts non-missing values per column. nunique() counts distinct values. (Lesson 4.1)

Joining and reshaping

17. merge vs join vs concat?

merge joins on columns (like SQL JOIN); join is a shortcut that joins on the index; concat stacks tables on top of each other or side by side. (Lessons 5.1, 5.2)

18. Inner, left, right, outer?

Inner keeps keys in both; left keeps all left rows; right all right rows; outer everything. indicator=True adds a column saying where each row came from, great for finding unmatched keys. (Lesson 5.2)

19. A merge added rows. Why?

The right table has more than one row per key, so each left row is repeated. Check with right.key.duplicated().sum() and protect with validate='many_to_one'. (Lesson 5.3)

20. Like VLOOKUP for one column?

df['category'] = df['product_id'].map(products.set_index('product_id')['category']). (Lesson 5.4)

21. Wide to long and back?

melt(id_vars=..., var_name=..., value_name=...) makes wide long; pivot or pivot_table makes long wide; stack/unstack move index levels to and from columns. (Lessons 6.1 to 6.3)

22. pivot vs pivot_table?

pivot only reshapes and fails on repeated index/column pairs; pivot_table aggregates repeats (aggfunc) and can add totals (margins=True). (Lessons 6.1, 6.3)

23. What is tidy data?

One row per observation, one column per variable, one table per kind of thing. Analyse long, show wide. (Lesson 6.5)

Dates and time series

24. Daily data to monthly totals?

df.resample('MS', on='date')['revenue'].sum(), or groupby(pd.Grouper(key='date', freq='MS')) next to other keys. (Lesson 7.2)

25. 7-day rolling average, and its catch?

daily.rolling(7).mean(). It assumes one row per day, so fill missing days first or use rolling('7D'); it lags by about half a window. (Lessons 7.1, 7.3)

26. Month over month and year over year growth?

monthly.pct_change() and monthly.pct_change(12); per group, groupby(...).pct_change(). Watch missing months and partial months. (Lesson 7.4)

27. Sales by weekday in the right order?

dt.day_name() as an ordered Categorical, or group on dt.dayofweek; compare per day, not totals. (Lesson 7.5)

Analyses

28. How do you compute average order value from order lines?

Total revenue divided by order_id.nunique(), never the mean of line revenue; rebuild it from totals at every level, including the total row. (Lesson 8.2)

29. Describe RFM in a minute.

Per customer: days since last order, number of orders and total spend; qcut each into 1-5 scores; name segments with rules (np.select); give each a different action. (Lesson 8.4)

30. How do you build a cohort retention table?

Cohort = first order month (transform('min')), age = months since then, count distinct customers by cohort and age, unstack, divide each row by month 0. (Lesson 8.5)

31. Analyse an A/B test in pandas?

Check the split, then groupby('group') for visitors, conversions and means; lift with a 95 % CI and p-value; guardrails; segments as a consistency check only. (Lesson 8.6)

32. Write ROW_NUMBER() OVER (PARTITION BY customer ORDER BY date).

df.sort_values('date').groupby('customer_id').cumcount() + 1. (Lesson 8.7)

Speed and good practice

33. Why avoid iterrows and apply(axis=1)?

They run Python code once per row; whole-column (vectorised) operations run in compiled code and are often hundreds of times faster. Use arithmetic on columns, np.where, np.select, map, .str. (Lesson 8.8)

34. A 5 GB CSV won't fit in memory. Options?

usecols and smaller dtypes (category, int32), chunksize to process in pieces, Parquet instead of CSV, or a tool built for it (DuckDB, Polars, Spark). (Lesson 8.8)

35. How do you make your analysis trustworthy and repeatable?

Method chains or small functions, assert checks after joins and cleaning, fixed snapshot dates, no manual edits, restart-and-run-all before sharing, and a README that states the question and the answer. (Lessons 8.1, 9.1)

Next: keep the cheat sheet open while you practise, and finish the capstone notebook so you have something to show.

New words: live coding, take-home test, vectorised, validate, named aggregation.

  • Name the grain of the answer first.
  • Talk through the plan, then write the line.
  • Use nunique for orders and customers.
  • End with a check: row count or a total.

Check yourself

1An interviewer asks for the top 3 products by revenue in each store. Which tools do you reach for?
2A live coding task: your merge returned more rows than the left table. What do you say?
3How do you answer when you forget the exact method name?

Sources

Your ticks stay in this browser. Save them as a file on the course home.

Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.