Pandas 中的多个 SELECT 计数(不同)

Multi SELECT COUNT(distinct) in Pandas

在SQL中:

SELECT distinct user_id
    , COUNT(product_id) AS c_products
    , COUNT(distinct product_id) AS c_distinct_product
FROM tables
GROUP BY user_id

我如何在 pandas 中做到这一点?

轻松:

df.groupby('user_id').agg({'product_id': ['count', pd.Series.nunique]})

Pandas 有帮助从 SQL 翻译成 Pandas 的文档:https://pandas.pydata.org/pandas-docs/stable/comparison_with_sql.html