[英]Pandas: how to calculate a rolling window over one column (grouped by date) and count distinct values of another column?
[英]How to do a distinct count of one field, grouped by another in Pandas
如果我想創建一個 dataframe,相當於 PANDAS 中的這個 SQL 怎么辦?
SELECT COUNTRY, COUNT(DISTINCT PRODUCT) AS UNIQUE_PRODUCTS
FROM SALES
GROUP BY COUNTY
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.