Reputation: 53
So I have a table that looks like this, and I would like to calculate the win rate after each game played. So for player 1, the win rate will start at 100% since he won the first game, then 50% since he lost the second game, then 33%, then back to 50% again.
PlayerId Status
1 1
1 0
1 0
1 1
so the final table will look something like this
PlayerId Status Win_Rate
1 1 100%
1 0 50%
1 0 33%
1 1 50%
Upvotes: 4
Views: 248
Reputation: 14847
You can .groupby
PlayerId and use expanding().mean()
:
In [12]: df.groupby("PlayerId").expanding().mean()
Out[12]:
PlayerId Status
PlayerId
1 0 1.0 1.000000
1 1.0 0.500000
2 1.0 0.333333
3 1.0 0.500000
2 4 2.0 1.000000
5 2.0 0.500000
6 2.0 0.333333
7 2.0 0.500000
You can then drop the index level and assign it as a column if you want it on your original frame:
In [18]: df['Win_Rate'] = df.groupby("PlayerId").expanding().mean()['Status'].droplevel(0)
In [19]: df
Out[19]:
PlayerId Status Win_Rate
0 1 1 1.000000
1 1 0 0.500000
2 1 0 0.333333
3 1 1 0.500000
4 2 1 1.000000
5 2 0 0.500000
6 2 0 0.333333
7 2 1 0.500000
If you want it as a string percentage, you can do extra formatting:
In [25]: df['Win_Rate'] = df['Win_Rate'].mul(100).apply(lambda x: '{}%'.format(int(x)))
In [26]: df
Out[26]:
PlayerId Status Win_Rate
0 1 1 100%
1 1 0 50%
2 1 0 33%
3 1 1 50%
4 2 1 100%
5 2 0 50%
6 2 0 33%
7 2 1 50%
Upvotes: 5