Python – Calculates unique values grouped in Python

Calculates unique values grouped in Python… here is a solution to the problem.

Calculates unique values grouped in Python

I’m using a python database and pandas. Currently my database shows as follows:

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

I can put it into a table that counts all Account_Num as follows:

Employer   Account_Num
AAA        3
BBB        2
CCC        1

I achieved the above using this code:

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

But I only need to calculate the unique Account_Num. It should look like this :

Employer   Account_Num
AAA        2
BBB        2
CCC        1

What is the best way to achieve this? Thanks!

Solution

You are looking for >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

Related Problems and Solutions