熊猫-计算重复值

时间:2020-07-25 18:25:43

标签: python pandas

与此:

df = pd.DataFrame({'players.name': ['John', 'Will' ,'John', 'Jim', 'Tim', 'John', 'Will', 'Tim'],
                    'players.diff': [0,0, 0, 0, 0, 0, 0, 0]})

但是'players.name'值重复了很多次,我需要计算每个唯一名称出现的次数os,并添加带有该值的列'count',最后是:

  players.name  players.diff  counts
0         John             0       3
1         Will             0       2
2         John             0       0
3          Jim             0       1
4          Tim             0       2
5         John             0       0
6         Will             0       2
7          Tim             0       2

我该怎么做?

0 个答案:

没有答案