smackenzie
smackenzie

Reputation: 3022

How to do a distinct count of one field, grouped by another in Pandas

If I wanted to create a dataframe, which is equivalent to this SQL in PANDAS how would I do it?

SELECT COUNTRY, COUNT(DISTINCT PRODUCT) AS UNIQUE_PRODUCTS 
FROM SALES
GROUP BY COUNTY

Upvotes: 0

Views: 48

Answers (1)

armamut
armamut

Reputation: 1116

df = pd.DataFrame({
    'Country': ['A', 'A', 'B', 'B', 'B', 'C', 'C'],
    'Product': ['X', 'X', 'X', 'Y', 'Z', 'Y', 'Z']    
})

df.groupby('Country').Product.nunique()
>>>
Country
A    1
B    3
C    2
Name: Product, dtype: int64

Upvotes: 3

Related Questions