Weighted vs Unweighted Segment Average: Mean of Segment Means vs Pooled Mean in Pandas
Quick Overview
Write a pandas function that compares the plain mean of per-segment weighted means with the pooled weighted mean and flags when the gap exceeds 1% of the pooled value. It tests groupby aggregation, weighted averages, exact output dtypes, and why averaging averages misleads on skewed segments.
Weighted vs Unweighted Segment Average: Mean of Segment Means vs Pooled Mean in Pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `weighted_vs_unweighted_segment_average(df, seg, metric, weight)` to compare the mean of per-segment means against the true pooled mean. `seg`, `metric` and `weight` are column names in `df`. Return a single-row DataFrame with a `RangeIndex` of `[0]` and these four columns:
- `unweighted_mean` (float64): the plain arithmetic mean of each segment's `weight`-weighted mean of `metric`, so every segment counts equally.
- `weighted_mean` (float64): `(df[metric] * df[weight]).sum() / df[weight].sum()`.
- `difference` (float64): `weighted_mean - unweighted_mean`.
- `misleading` (bool): `True` when `abs(difference) > 0.01 * abs(weighted_mean)`.
The result is compared with `assert_frame_equal`, so the dtypes must be exactly float64 and bool.
Example with `seg='region'`, `metric='rate'` and `weight='n'`:
```text
region rate n
A 0.08 500
A 0.12 500
B 0.90 20
C 0.90 20
D 0.90 20
```
Expected result (values rounded to six decimals):
```text
unweighted_mean weighted_mean difference misleading
0 0.700000 0.145283 -0.554717 True
```
Region A's weighted mean is 0.10 and regions B, C and D are 0.90 each, so the mean of means is (0.10 + 0.90 + 0.90 + 0.90) / 4 = 0.70. The pooled mean counts every unit once: 154 / 1060 is about 0.145283. The gap is far larger than 1% of the pooled mean, so `misleading` is `True`.
```hint Per-segment means
A segment can have many rows with different weights. Think about which two group-level sums give you that segment's weighted mean exactly.
```
```hint Dtype pitfall
`assert_frame_equal` checks dtypes as well as values. Look at what type each scalar has before you put it in the DataFrame.
```
### Constraints & Assumptions
- `df` can have any number of rows per segment, and `seg` can be a string, integer or categorical column.
- Weights are non-negative, and every segment's weights sum to a positive number.
- The output has exactly one row, a default `RangeIndex` of `[0]`, and the four columns in the order listed.
### What a Strong Answer Covers
- Computing each segment's mean as a weighted mean from summed products and summed weights, then averaging those segment means with equal weight.
- Computing the pooled mean over the raw rows and explaining why it moves away from the mean of means when segment sizes are lopsided.
- Applying the 1% relative threshold with absolute values so the sign of the gap does not matter.
- Returning the exact shape and dtypes: one row, `RangeIndex` `[0]`, float64 columns and a bool flag.
- Edge cases such as missing segment keys, missing metric values and segments with zero total weight.
### Follow-up Questions
- When are the two averages actually equal?
- How would you handle a segment where `weight` sums to zero?
- The threshold scales with `abs(weighted_mean)`. What goes wrong when the pooled mean is zero or close to it, and how would you change the rule?
Overview: Write a pandas function that compares the plain mean of per-segment weighted means with the pooled weighted mean and flags when the gap exceeds 1% of the pooled value. It tests groupby aggregation, weighted averages, exact output dtypes, and why averaging averages misleads on skewed segments.
Write weighted_vs_unweighted_segment_average(df, seg, metric, weight) to compare the mean of per-segment means against the true pooled mean. seg, metric and weight are column names in df. Return a single-row DataFrame with a RangeIndex of [0] and these four columns:
unweighted_mean
(float64): the plain arithmetic mean of each segment's
weight
-weighted mean of
metric
, so every segment counts equally.
Region A's weighted mean is 0.10 and regions B, C and D are 0.90 each, so the mean of means is (0.10 + 0.90 + 0.90 + 0.90) / 4 = 0.70. The pooled mean counts every unit once: 154 / 1060 is about 0.145283. The gap is far larger than 1% of the pooled mean, so misleading is True.
Constraints & Assumptions
df
can have any number of rows per segment, and
seg
can be a string, integer or categorical column.
Weights are non-negative, and every segment's weights sum to a positive number.
The output has exactly one row, a default
RangeIndex
of
[0]
, and the four columns in the order listed.
What a Strong Answer Covers Guidance
Computing each segment's mean as a weighted mean from summed products and summed weights, then averaging those segment means with equal weight.
Computing the pooled mean over the raw rows and explaining why it moves away from the mean of means when segment sizes are lopsided.
Applying the 1% relative threshold with absolute values so the sign of the gap does not matter.
Returning the exact shape and dtypes: one row,
RangeIndex[0]
, float64 columns and a bool flag.
Edge cases such as missing segment keys, missing metric values and segments with zero total weight.
Follow-up Questions Guidance
When are the two averages actually equal?
How would you handle a segment where
weight
sums to zero?
The threshold scales with
abs(weighted_mean)
. What goes wrong when the pooled mean is zero or close to it, and how would you change the rule?