Reputation: 3022
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
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