Let me try and explain what I need help with. Im not the best so please bear with :)
I have a tennis dataset which has player 1 and player 2, sometimes a player is player 1 and sometimes they are player 2.
I have a two stats which I am trying to find the rolling average for both of them.
This is currently what I have
| P1 | P2 | P1_1stWon | P2_1stWon |
|---|---|---|---|
| Nadal | Murray | 5 | 6 |
| Murray | Nadal | 7 | 4 |
and this is what im after
| P1 | P2 | P1_1stWon | P2_1stWon | P1_1stWonAvg | P2_1stWonAvg |
|---|---|---|---|---|---|
| Nadal | Murray | 5 | 6 | 5 | 6 |
| Murray | Nadal | 7 | 4 | 6.5 | 4.5 |
So you can see that for example Nadal is P1 in the first game and scores 5 in the second game he is P2 and scores 4, therefore his average is 4.5 and as he is P2 in the second game you can see his avg score in the P2_1stWonAvg.
This is current the code I have but its not working. Any help is much appreciated :)
df['P1_1stWonAvg'] = df.groupby(['P1', 'P2'])['P1_1stWon', 'P2_1stWon'].apply(pd.rolling, 2, min_periods=2)