【问题标题】:Multi SELECT COUNT(distinct) in PandasPandas 中的多 SELECT COUNT(distinct)
【发布时间】:2017-10-23 14:53:03
【问题描述】:

在 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 中做到这一点?

【问题讨论】:

    标签: python group-by count distinct pandas-groupby


    【解决方案1】:

    轻松:

    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

    【讨论】:

    • 非常感谢,但我想创建新列,所以我使用了df['c_products'] = df.groupby(grouped_on)['product_id'].transform(lambda v: v.count()).astype(int)df['c_distinct_product'] = df.groupby(grouped_on)['product_id'].transform(pd.Series.nunique).astype(int)'
    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2013-09-28
    • 2017-12-19
    • 2012-05-10
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多