[英]Add column with accumulative count of unique values in a column
I'm trying to add a column with the number of unique values appearances, so if my input is:我正在尝试添加具有唯一值出现次数的列,因此如果我的输入是:
id | score
asd 15
asd 20
jk 76
pz 54
pz 37
I want to add a column like this我想添加一个这样的列
id | score | count_ids
asd 15 1
asd 20 1
jk 76 2
pz 54 3
pz 37 3
I've seen this answer Pandas add unique count column , although I feel like there should be a neater solution to this.我已经看到这个答案Pandas add unique count column ,虽然我觉得应该有一个更简洁的解决方案。
Edit
编辑
I'm also looking for a scalable solution, so for example, if I also have this:我也在寻找一个可扩展的解决方案,例如,如果我也有这个:
id | game | score
asd 1 15
asd 2 20
jk 1 76
pz 1 54
pz 1 37
Get得到
id | game | score | count_users
asd 1 15 1
asd 2 20 2
jk 1 76 3
pz 1 54 4
pz 1 37 4
I think solution should be simplify without helper column:我认为解决方案应该在没有帮助列的情况下简化:
df['count_ids'] = df['id'].ne(df['id'].shift()).cumsum()
print (df)
id score count_ids
0 asd 15 1
1 asd 20 1
2 jk 76 2
3 pz 54 3
4 pz 37 3
EDIT: For multiple columns solution is similar, only add DataFrame.any
for test at least one True
per row:编辑:对于多列解决方案是类似的,只需添加
DataFrame.any
以测试每行至少一个True
:
df['count_ids'] = df[['id', 'game']].ne(df[['id', 'game']].shift()).any(axis=1).cumsum()
print (df)
id game score count_ids
0 asd 1 15 1
1 asd 2 20 2
2 jk 1 76 3
3 pz 1 54 4
4 pz 1 37 4
Detail :详情:
print (df[['id', 'game']].ne(df[['id', 'game']].shift()))
id game
0 True True
1 False True
2 True True
3 True False
4 False False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.