I've been scratching my head about this one for a little while.
I've got a data frame which looks like this:
import pandas as pd import numpy as np
d={'gameweek': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 2, 9: 2, 10: 2, 11: 2, 12: 2, 13: 2, 14: 2, 15: 2, 16: 3, 17: 3, 18: 3, 19: 3, 20: 3, 21: 3, 22: 3, 23: 3}, 'match_id': {0: 0, 1: 0, 2: 1, 3: 1, 4: 2, 5: 2, 6: 3, 7: 3, 8: 4, 9: 4, 10: 5, 11: 5, 12: 6, 13: 6, 14: 7, 15: 7, 16: 8, 17: 8, 18: 9, 19: 9, 20: 10, 21: 10, 22: 11, 23: 11}, 'points': {0: 48.0, 1: np.nan, 2: 41.0, 3: 40.0, 4: 55.0, 5: 50.0, 6: 38.0, 7: 45.0, 8: 40.0, 9: 37.0, 10: 29.0, 11: np.nan, 12: 43.0, 13: 15.0, 14: 46.0, 15: 43.0, 16: 24.0, 17: 45.0, 18: 40.0, 19: 20.0, 20: 45.0, 21: np.nan, 22: 49.0, 23: 35.0}, 'name': {0: 'team1', 1: 'Average', 2: 'team2', 3: 'team3', 4: 'team4', 5: 'team5', 6: 'team6', 7: 'team7', 8: 'team2', 9: 'team7', 10: 'team5', 11: 'Average', 12: 'team1', 13: 'team3', 14: 'team6', 15: 'team4', 16: 'team5', 17: 'team1', 18: 'team7', 19: 'team3', 20: 'team6', 21: 'Average', 22: 'team2', 23: 'team4'}}
df = pd.DataFrame.from_dict(data=d)
df.head()
gameweek match_id name points
0 1 0 team1 48.0
1 1 0 Average NaN
2 1 1 team2 41.0
3 1 1 team3 40.0
4 1 2 team4 55.0
What I'd like to do is replace all of the 'NaNs' with the mean of the grouped gameweek columns.
I can work out how to get the mean using a groupby function, e.g.
df['points'].groupby(by=df['gameweek']).mean()
gameweek
1 45.285714
2 36.142857
3 36.857143
Name: points, dtype: float64
But I'm struggling to then apply that to the correct field for each 'gameweek' column. I've looked at using 'fillna' but can't work out how to target a specific row with a specific value.
Any help would be appreciated. Many thanks.