简体   繁体   English

计算在Python中分组的唯一值

[英]Count unique values that are grouped by in Python

I am using a python database and am using pandas. 我正在使用python数据库并使用pandas。 Currently my database shows something like this: 目前我的数据库显示如下:

Employer        Account_Num
AAA             123
BBB             456
AAA             789
AAA             123
BBB             101
CCC             112

I am able to put it into a table that counts all the Account_Num, which looks like this: 我可以将它放入一个计算所有Account_Num的表中,它看起来像这样:

Employer   Account_Num
AAA        3
BBB        2
CCC        1

I used this code to achieve the above: 我用这段代码来实现上述目的:

bigdata.groupby(['Employer'])[['Account_Num']].count()

But I only need the unique Account_Num's counted. 但我只需要计算唯一的Account_Num。 Which should look something like this: 哪个应该是这样的:

Employer   Account_Num
AAA        2
BBB        2
CCC        1

What is the best way I can achieve this? 我能做到这一点的最佳方式是什么? Thank you! 谢谢!

You're looking for nunique() . 你正在寻找nunique()

df.groupby('Employer').Account_Num.nunique()

Demo 演示

>>> df.groupby('Employer').Account_Num.nunique()

Employer
AAA    2
BBB    2
CCC    1
Name: Account_Num, dtype: int64

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

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