[英]How to count unique records by two columns per group in pandas?
Same as How to count unique records by two columns in pandas? 与如何通过pandas中的两列计算唯一记录相同? , only per group.
,仅限每组。 I tried:
我试过了:
df = pd.DataFrame({'a': [1,1,1,2,2], 'b':[10,10,20,30,30], 'c':[5,7,7,11,17]})
df.groupby('a').groupby(['b', 'c']).ngroups
And it throws AttributeError
. 它会抛出
AttributeError
。
You don't need the double groupby: Use drop_duplicates
with ['b', 'c']
as your subset, to keep only unique rows, then groupby 'a'
and use size
: 你不需要double groupby:使用带有
['b', 'c']
drop_duplicates
作为你的子集,只保留唯一的行,然后groupby'a 'a'
并使用size
:
df.drop_duplicates(['b', 'c']).groupby('a').size()
a
1 3
2 2
dtype: int64
You need to apply a function to the results of first groupping: 您需要将函数应用于第一次灌浆的结果:
df.groupby('a').apply(lambda x: x.groupby(['b', 'c']).ngroups)
#a
#1 3
#2 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.