I have this dataframe:
| Date | Position | TrainerID | Win% |
|---|---|---|---|
| 2017-09-03 | 4 | 1788 | 0 (0 wins, 1 race) |
| 2017-09-16 | 5 | 1788 | 0 (0 wins, 2 races) |
| 2017-10-14 | 1 | 1788 | 33 (1 win, 3 races) |
I want to compute on every row of the Win% Column the winning percentage, as above, for the races in the last 1000 days.
I tried something like this:
def compute_winning_percentage(a, b):
return (a / b)*100
featured_data['Percentage win of trainer in the last 1000 days'] = featured_data.groupby('TrainerID').apply(
compute_winning_percentage(len(featured_data.loc[featured_data.Position == 1]),
featured_data[featured_data.Position].cumcount()))
But I get an error and I have no idea how to insert the last 1000 days part.
How should I do it?