[英]Python divide cell by sum of group
New to python and just hoping to get some help python 的新手,希望得到一些帮助
I have a data frame that is players, team and number of shots in a game.我有一个数据框,它是球员、球队和比赛中的投篮次数。 I want to create a column that is the proportion of a team's shots a player takes So my data frame looks something like this我想创建一个列,表示球员在球队投篮中所占的比例所以我的数据框看起来像这样
Player播放器 | Team团队 | Shots镜头 |
---|---|---|
PlayA玩A | A一种 | 4 4个 |
PlayB播放B | A一种 | 6 6个 |
PlayC播放C | B乙 | 5 5个 |
PlayD播放D | B乙 | 15 15 |
and what I want is我想要的是
Player播放器 | Team团队 | Shots镜头 | Shot Prop射击道具 |
---|---|---|---|
PlayA玩A | A一种 | 4 4个 | 0.4 0.4 |
PlayB播放B | A一种 | 6 6个 | 0.6 0.6 |
PlayC播放C | B乙 | 5 5个 | 0.25 0.25 |
PlayD播放D | B乙 | 15 15 | 0.75 0.75 |
But in reality there is no set number of players or teams但实际上没有固定数量的球员或球队
Any help is very appreciated非常感谢任何帮助
Thanks, Dave谢谢,戴夫
Use Series.div
with GroupBy.transform
and sum
for repeat aggregate values to Series wit same size like original:将Series.div
与GroupBy.transform
和sum
一起使用,以将聚合值重复到与原始大小相同的系列:
df['Short Prop'] = df['Short'].div(df.groupby('Team')['Short'].transform('sum'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.