[英]Sum over string column python
I am struggling to count the size of a team. 我正在努力计算一个团队的规模。 I have for every individual, the name of the team she belongs to.
对于每个人,我都有她所属团队的名字。
There are however hundreds of different teams. 但是,有数百个不同的团队。 I want to construct another column that would count the number of individuals that belong to a given team.
我想构建另一列,该列将计算属于给定团队的个人数量。 I have been able to do a similar operation over a numeric column (using .transform(sum) ), but I can't figure out how to do it when counting over strings.
我已经能够对数字列执行类似的操作(使用.transform(sum)),但是在计算字符串时我无法弄清楚该如何做。
Please do let me know if anything is unclear ! 如果有任何不清楚的地方,请告诉我!
Are you trying to do it with Pandas? 您是否正在尝试使用Pandas? If so, try:
如果是这样,请尝试:
df1['count'] = df1.groupby('team', as_index=False)['team'].transform(lambda s: s.count())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.