首页 > 解决方案 > 按每个唯一 ID 分组,然后查找每个品牌的购买次数

问题描述

我有过去 3 年的客户购买数据。下面的示例:

 customer_id|date      |sales_amount|product_type
 479485     |20190120  | 500        | bags
 479485     |20180320  | 200        | clothes
 479485     |20180321  | 200        | clothes
 472848     |20191020  | 100        | clothes

我想为每个唯一的客户 ID 查找他们在三年内针对不同产品类型进行的交易次数。理想情况下,每个唯一客户 ID 的产品类型的价值计数。所以对于customer_id= 479485 的输出:

 customer_id
 479485     |bags      | 1       
            |clothes   | 2   

我试过做一个数据透视表,但它没有给我理想的结果:

pd.pivot_table(clusters, index=['customer_id', 'product_type'], aggfunc='sum')

奖金:如果我想做同样的事情,但看它但按年份分开,这可能吗?

标签: pythonpandasgroup-bypandas-groupby

解决方案


假设输入数据是这样的:

df=pd.DataFrame({'cust_id':[479485,479485,479485,472848],
                 'date':['20190120','20180320','20180321','20191020'],
                 'sales_amount':[500,200,200,100],
                 'product_type':['bags','clothes','clothes','clothes']})

我会做这样的事情:

df.groupby(['cust_id','product_type'])['sales_amount'].count()

按年份分组当然是可能的。有几个选项,但您必须查看日期时间库以将日期列转换为日期时间对象,然后进行处理。


推荐阅读