Python 将groupby单独规范化

Python 将groupby单独规范化,python,pandas,Python,Pandas,我想知道一个球员在总比赛中输赢的百分比。分析的列将为: 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 输出如下所示: player | games | wins | losses  

我想知道一个球员在总比赛中输赢的百分比。分析的列将为:

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
输出如下所示:

player | games | wins | losses
  John |   4   |  50% | 50%
  Jose |   2   | 100% | 0%
  Adam |   1   |   0% | 100%
用于计数器和与表连接的


在分组数据帧上使用
应用
的替代解决方案:

df_out = df.groupby('player')   \
           .apply(lambda x: pd.Series({'games': len(x.game),
                                       'wins': 100*sum(x.games == 'has won')/ len(x.game),
                                       'losses': 100*sum(x.games == 'lost')/ len(x.game)})) \
           .reset_index()

print(df_out)
   player  games   wins  losses
0  Adam      1.0    0.0   100.0
1  John      4.0   50.0    50.0
2  Jose      2.0  100.0     0.0

耸人听闻!我试着把输赢分开,然后恢复正常,但你这样做看起来很棒。非常感谢。我不明白normalize=0,你能解释一下吗?
df_out = df.groupby('player')   \
           .apply(lambda x: pd.Series({'games': len(x.game),
                                       'wins': 100*sum(x.games == 'has won')/ len(x.game),
                                       'losses': 100*sum(x.games == 'lost')/ len(x.game)})) \
           .reset_index()

print(df_out)
   player  games   wins  losses
0  Adam      1.0    0.0   100.0
1  John      4.0   50.0    50.0
2  Jose      2.0  100.0     0.0