I have the following data frame:
| HorseId | FGrating | Average FGrating |
|---|---|---|
| 1736 | 110 | -1 |
| 1736 | 124 | -1 |
| 1736 | 118 | -1 |
| 13973 | 144 | -1 |
| 13973 | 137 | -1 |
I want to fill the Average FGrating column with the average FGrating of every horse up to the point it was computed, grouped by HorseId. The result that I am looking for is this:
| HorseId | FGrating | Average FGrating |
|---|---|---|
| 1736 | 110 | 110 |
| 1736 | 124 | 117 (110+124)/2 |
| 1736 | 118 | 117.3 (110+124+118)/3 |
| 13973 | 144 | 144 |
| 13973 | 137 | 140.5 (144+137)/2 |
The code I used to solve this problem is:
featured_data['Average FGrating'] = featured_data[['HorseId', 'FGrating']].groupby('HorseId')[
'FGrating'].mean()
However, it computes the desired average in only a part of the data frame not in all of it.
What am I doing wrong? How can I solve this problem?