Build a two-dimensional segment lift report in pandas (cell mean vs overall mean)
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `two_dim_segment_lift(df, seg1, seg2, metric)` that crosses two segment columns and returns a long DataFrame, with one row per observed `(seg1, seg2)` cell and no pivot. Each row measures how far that cell's mean of `metric` deviates from the pooled overall mean.
Requirements:
- Columns in this order: `seg1_value` (object), `seg2_value` (object), `n` (int64), `cell_mean`, `overall_mean` and `pct_lift` (all float64).
- `overall_mean` is the pooled mean of the whole `metric` column, repeated on every row.
- `pct_lift = (cell_mean - overall_mean) / overall_mean * 100`.
- Only cells that occur in the data appear (no zero-row combinations).
- Sort by `pct_lift` ascending, then `seg1_value` ascending, then `seg2_value` ascending, and finish with `reset_index(drop=True)`.
Example:
```python
df = pd.DataFrame({
"platform": ["ios", "ios", "ios", "android", "android", "android"],
"country": ["US", "US", "CA", "US", "US", "CA"],
"revenue": [13.0, 13.0, 4.0, 7.0, 7.0, 16.0],
})
two_dim_segment_lift(df, "platform", "country", "revenue")
# seg1_value seg2_value n cell_mean overall_mean pct_lift
# 0 ios CA 1 4.0 10.0 -60.0
# 1 android US 2 7.0 10.0 -30.0
# 2 ios US 2 13.0 10.0 30.0
# 3 android CA 1 16.0 10.0 60.0
```
The overall mean is 60 / 6 = 10. Both platforms and both countries also average exactly 10 on their own, so neither dimension looks unusual until the two are crossed.
```hint Check what the groupby hands back
Look at the dtypes of the key columns and the counts after grouping, and at what happens when a segment column is categorical.
```
```hint Decide which rows count
Work out whether a row with a missing `metric` value counts toward its cell's `n`, and whether rows with a missing segment value still belong in the baseline.
```
### Constraints & Assumptions
- `df` is a pandas DataFrame. `seg1` and `seg2` hold segment labels (strings, categories or integers) and `metric` is numeric.
- The result is compared with `assert_frame_equal`, so the dtypes and the column order must match exactly.
### Clarifying Questions to Ask
- Should `n` count every row in a cell, or only rows with a non-null `metric`?
- What should `pct_lift` be when the overall mean is 0 and the ratio is undefined?
- Should rows with a missing segment value form their own cell, or be left out of the report?
### What a Strong Answer Covers
- How the pooled baseline is computed and why it is the same on every row.
- How the cells are built so that only observed combinations appear, including when a segment column is categorical.
- The output contract: the column order, each column's dtype and the sort with its tie-breakers.
- The handling of missing values and of a zero overall mean.
- What a comparison against the pooled mean can and cannot say about interaction effects, and the time and space cost.
### Follow-up Questions
- How would you flag cells whose lift may just be noise from a small `n`?
- How would you extend this to more than two dimensions without an explosion of rows?
- A cell shows a large negative lift. How would you tell whether that comes from one segment alone or from the combination of the two?
Overview: Write a pandas function that crosses two segment columns and reports, for every observed cell, its size, its mean and its percent lift against the pooled overall mean, sorted worst first. It tests groupby aggregation, exact dtypes, deterministic tie-breaking and how to read interaction effects.