Understand the idea
transform returns a group statistic at every original row. This lets each observation be compared with its own group.
A small example
For A: 2, A: 6, B: 9, transform("mean") returns 4, 4, 9; subtracting gives −2, 2, 0.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
df["group_mean"] = df.groupby("flavour")["price"].transform("mean")
dfWhat each part does
df.groupby("flavour")- Make one group for each flavour.
["price"]- Select the price values to summarise inside each group.
.transform("mean")- Calculate each group mean and repeat it at every original row in that group.
df["group_mean"] =- Store those aligned means in a new column beside the original rows.
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| candy | flavour | price | rating | shelf |
|---|---|---|---|---|
| Gummy Bear | fruity | 1.2 | 4.1 | A |
| Choco Pop | chocolate | 2.1 | 4.6 | B |
| Mint Bite | mint | 1.5 | 3.8 | A |
| Berry Loop | fruity | 2.8 | 4.4 | B |
| Cocoa Cube | chocolate | 3.4 | 4.9 | A |
| Lemon Drop | fruity | 1.8 | 4 | B |
Your task · Follow
- Using df, add group_mean containing the mean price for each row’s flavour category.
- Preserve all rows.
- Keep the changes in df and display it.
- Use: transform().
Hint
transform keeps one result per original row; agg would reduce each group.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
df["group_mean"] = df.groupby("flavour")["price"].transform("mean")
df