Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalize groupby individually

Tags:

python

pandas

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%
like image 630
little grasshopper Avatar asked Mar 04 '23 16:03

little grasshopper


1 Answers

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
like image 140
jezrael Avatar answered Mar 19 '23 07:03

jezrael