我有一个像这样的数据框:
id-customer status
1 Won
1 Won
2 Won
2 Won
3 Not won
我想添加一个新列,其赢率计算如下:“获胜”数量由id-customer分组的总数划分。
所以我应该有一个像这样的数据框:
id-customer status winrate
1 Won 1 (2/2)
1 Won 1 (2/2)
2 Won 1 (2/2)
2 Won 1 (2/2)
3 Not won 0 (0/1)
我这样尝试:
df_winrate=df.groupby(['id-customer'],as_index=False)['status'].sum()
但这没用
可以帮我计算winrate列吗?
谢谢
答案 0 :(得分:0)
使用groupby
,只需使用sum
计算size
与transform
的比率,即可将结果广播到原始大小。
g = df.status.eq('Won').groupby(df['id-customer'])
g.transform('sum')/g.transform('size')
0 1.0
1 1.0
2 1.0
3 1.0
4 0.0