I want to know the percentage of how much a player has won and lost by the total games he has played. The columns analyzed will be:
game | player | games
aXa | Jose | has won
aXb | John | has won
aXb | John | has won
uXu | Adam | lost
bXb | John | lost
oXo | John | lost
pXp | Jose | has won
The output would look like this:
player | games | wins | losses
John | 4 | 50% | 50%
Jose | 2 | 100% | 0%
Adam | 1 | 0% | 100%
Use Series.value_counts
for counter and join
with crosstab
table:
df2 = (df['player'].value_counts().rename_axis('player').to_frame('games')
.join(pd.crosstab(df['player'], df['games'], normalize=0).mul(100))
.reset_index())
print (df2)
player games has won lost
0 John 4 50.0 50.0
1 Jose 2 100.0 0.0
2 Adam 1 0.0 100.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With