简体   繁体   English

计算熊猫组中的唯一值

[英]count unique values in groups pandas

I have a dataframe like this:我有一个这样的数据框:

data = {'id': [1,1,1,2,2,3],
        'value': ['a','a','a','b','b','c'],
        'obj_id': [1,2,3,3,3,4]
}
df = pd.DataFrame (data, columns = ['id','value','obj_id'])

I would like to get the unique counts of obj_id groupby id and value :我想获得obj_id groupby idvalue的唯一计数:

1 a 3
2 b 1
3 c 1

But when I do:但是当我这样做时:

result=df.groupby(['id','value'])['obj_id'].nunique().reset_index(name='obj_counts')

the result I got was:我得到的结果是:

1 a 2
1 a 1
2 b 1
3 c 1

so the first two rows with same id and value don't group together.所以具有相同idvalue的前两行不会组合在一起。

How can I fix this?我怎样才能解决这个问题? Many thanks!非常感谢!

For me your solution working nice with sample data.对我来说,您的解决方案适用于示例数据。

Like mentioned @YOBEN_S in comments is possible problem traling whitespeces, then solution is add Series.str.strip :就像在评论中提到的@YOBEN_S 可能是跟踪 whitespeces 的问题,然后解决方案是添加Series.str.strip

data = {'id': [1,1,1,2,2,3],
        'value': ['a ','a','a','b','b','c'],
        'obj_id': [1,2,3,3,3,4]
}
df = pd.DataFrame (data, columns = ['id','value','obj_id'])

df['value'] = df['value'].str.strip()
df = df.groupby(['id','value'])['obj_id'].nunique().reset_index(name='obj_counts')
print (df)
   id value  obj_counts
0   1     a           3
1   2     b           1
2   3     c           1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM