Fill missing values with the group mean
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
Some players have no `score`. Fill each missing score with the mean score of that player's team (computed from the team's known scores) and return the `score` column as a Series.
What this exercise teaches
fillna accepts a Series and uses the value with the matching label for each gap, so transform("mean") supplies each row its own team's mean. mean skips NaN, so the known scores define the fill. Filling with the overall mean ignores the team, and returning the transform alone overwrites the real scores too.
A hint if you get stuck
Build a Series of team means aligned to every row with transform("mean"), then pass it to fillna.
Try it now, free, no account needed
The first 15 tasks on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.
Open this exerciseMore exercises in Aggregating
- Total by groupPro
- Group means as a flat tablePro
- Count rows per groupPro
- Named aggregationsPro
- Group by two keysPro
- Pivot table with a fill valuePro
- Counts with crosstabPro
- Shares with value_countsPro
- Several statistics at oncePro
- Share of the group total with transformPro
- Largest order per rep with idxmaxPro
- Pivot table with totalsPro