Mean-center a pandas column within groups using groupby transform
Quick Overview
Add a column to a pandas DataFrame that holds each row's value minus the mean value of its group, using groupby with transform. It tests how transform broadcasts group results back to the original rows, index alignment, and returning a new frame without mutating the input.
Mean-center a pandas column within groups using groupby transform
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `add_group_mean_diff(df)`. Given columns `group` and `value`, return a copy of `df` with a new column `value_minus_group_mean`: each row's `value` minus the mean `value` of its `group`. Broadcast the group mean with `transform`; do not use `agg`.
Requirements:
- Return a new DataFrame. Do not mutate the input.
- Keep the same index and row order as `df`.
For example:
```python
df = pd.DataFrame({'group': ['a', 'a', 'b'], 'value': [10, 20, 5]})
add_group_mean_diff(df)
# group value value_minus_group_mean
# 0 a 10 -5.0
# 1 a 20 5.0
# 2 b 5 0.0
```
```hint Line the means up with the rows
The group means have to match the original rows by index before you subtract. Also check that adding the new column does not write into the caller's `df`.
```
### What a Strong Answer Covers
- A same-length, same-index Series of group means produced by `transform`.
- An index-aligned subtraction so each row is centered on its own group's mean.
- A way to attach the column without mutating the input, with the index and row order unchanged.
- How `NaN` values in `value` and missing group keys are handled.
### Follow-up Questions
- How does `transform('mean')` differ from `agg('mean')` here?
- What happens to `NaN` values in `value`, and how would you control it?
- How would you standardize `value` within each group (a per-group z-score) instead of only centering it?
Overview: Add a column to a pandas DataFrame that holds each row's value minus the mean value of its group, using groupby with transform. It tests how transform broadcasts group results back to the original rows, index alignment, and returning a new frame without mutating the input.
Implement add_group_mean_diff(df). Given columns group and value, return a copy of df with a new column value_minus_group_mean: each row's value minus the mean value of its group. Broadcast the group mean with transform; do not use agg.
Requirements:
Return a new DataFrame. Do not mutate the input.
Keep the same index and row order as
df
.
For example:
df = pd.DataFrame({'group': ['a', 'a', 'b'], 'value': [10, 20, 5]})
add_group_mean_diff(df)
# group value value_minus_group_mean
# 0 a 10 -5.0
# 1 a 20 5.0
# 2 b 5 0.0
What a Strong Answer Covers Guidance
A same-length, same-index Series of group means produced by
transform
.
An index-aligned subtraction so each row is centered on its own group's mean.
A way to attach the column without mutating the input, with the index and row order unchanged.
How
NaN
values in
value
and missing group keys are handled.
Follow-up Questions Guidance
How does
transform('mean')
differ from
agg('mean')
here?
What happens to
NaN
values in
value
, and how would you control it?
How would you standardize
value
within each group (a per-group z-score) instead of only centering it?