Python for Data course menu
Intermediate pandas and Statistics Interview Questions
Intermediate data analyst interview questions with pandas solutions: groupby transform vs agg, pivot tables, top N per group, rolling averages, month-over-month growth, mean vs median, outliers, correlation vs causation and p-values.
On this page
- pandas
- 1. What is the difference between groupby().agg() and groupby().transform()?
- 2. Find the top 2 products by revenue in each region.
- 3. Compute month-over-month growth.
- 4. Build a pivot table of revenue by region and month with totals.
- 5. Why is iterating over rows with iterrows slow, and what do you use instead?
- Statistics
- 6. When would you report the median instead of the mean?
- 7. How do you detect outliers?
- 8. Correlation is 0.85 between ice-cream sales and sunburn cases. What do you conclude?
- 9. Explain a p-value to a non-technical manager.
- 10. What is the difference between a Type I and a Type II error?
- Key takeaway
These questions test whether you can combine pandas operations and reason about statistics: the core of data analyst technical rounds.
pandas#
1. What is the difference between groupby().agg() and groupby().transform()?#
Show solution
agg returns one row per group; transform returns a value for every original row, aligned with the input. Use transform to compare rows with their group:
import pandas as pd
df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].agg("mean").to_dict())
print(df.groupby("team")["score"].transform("mean").tolist()){'A': 15.0, 'B': 30.0}
[15.0, 15.0, 30.0]2. Find the top 2 products by revenue in each region.#
Show solution
Sort by revenue, then take the first rows of each group:
import pandas as pd
sales = pd.DataFrame({
"region": ["N", "N", "N", "S", "S", "S"],
"product": ["Pen", "Book", "Bag", "Pen", "Book", "Bag"],
"revenue": [300, 900, 450, 200, 150, 800],
})
top = sales.sort_values("revenue", ascending=False).groupby("region").head(2)
print(top.sort_values(["region", "revenue"], ascending=[True, False])) region product revenue
1 N Book 900
2 N Bag 450
5 S Bag 800
3 S Pen 200groupby(...).head(n) keeps the first n rows of each group in their current order. An alternative is rank with transform-style grouping and filtering rank <= 2.
3. Compute month-over-month growth.#
Show solution
import pandas as pd
monthly = pd.Series([120, 150, 135], index=["Jan", "Feb", "Mar"])
print((monthly.pct_change() * 100).round(1).tolist())[nan, 25.0, -10.0]With several groups (stores), use groupby("store")["revenue"].pct_change() so growth does not cross from one store into the next.
4. Build a pivot table of revenue by region and month with totals.#
Show solution
import pandas as pd
df = pd.DataFrame({"region": ["N", "S", "N", "S"], "month": ["Jan", "Jan", "Feb", "Feb"],
"revenue": [100, 80, 120, 90]})
print(pd.pivot_table(df, index="region", columns="month", values="revenue",
aggfunc="sum", margins=True, margins_name="Total"))month Feb Jan Total
region
N 120 100 220
S 90 80 170
Total 210 180 390Always set aggfunc explicitly: the default is the mean.
5. Why is iterating over rows with iterrows slow, and what do you use instead?#
Show solution
iterrows runs Python code once per row and builds a Series for each, which is slow on large data. Prefer vectorised column operations (df["a"] * df["b"]), np.where/np.select for conditions, .str and .dt methods, groupby/merge, and only then apply.
Statistics#
6. When would you report the median instead of the mean?#
Show solution
When the data is skewed or has outliers, such as incomes, house prices or delivery times. One very large value pulls the mean up but barely moves the median:
import pandas as pd
salaries = pd.Series([40, 45, 50, 55, 400])
print(salaries.mean(), salaries.median())118.0 50.07. How do you detect outliers?#
Show solution
Plot first (box plot, histogram). Then use a rule: the IQR rule flags values below Q1 − 1.5·IQR or above Q3 + 1.5·IQR and is robust; z-scores beyond ±3 assume roughly normal data and are themselves inflated by the outliers. Investigate before removing: errors should be fixed or removed, real extremes kept or analysed separately.
8. Correlation is 0.85 between ice-cream sales and sunburn cases. What do you conclude?#
Show solution
That they move together strongly, not that one causes the other. A confounder (hot, sunny weather) drives both. Establishing causation needs a controlled experiment (like an A/B test) or careful causal analysis.
9. Explain a p-value to a non-technical manager.#
Show solution
"If the change made no difference at all, how often would we see a gap this big just by luck?" A p-value of 0.02 means about 2% of the time. Small p-values mean the result is hard to explain by luck alone. It is not the probability that the change works, and it says nothing about whether the effect is big enough to matter.
10. What is the difference between a Type I and a Type II error?#
Show solution
A Type I error is a false positive: concluding there is an effect when there is none (its rate is α, often 5%). A Type II error is a false negative: missing a real effect, usually because the sample was too small. Larger samples reduce Type II errors; a lower α reduces Type I errors at the cost of more Type II errors.
Key takeaway#
Next: Advanced Questions.
Finished this lesson?