[英]Group by each unique id and then find number of purchases for each brand
我有过去 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')
奖金:如果我想做同样的事情但看它但按年份分开这可能吗?
假设输入数据是这样的:
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()
按年份分组当然是可能的。 有多种选择,但您必须查看日期时间库以将日期列转换为日期时间对象,然后再进行处理。
这应该有效:
temp = df.groupby(['customer_id', 'product_type'])['date'].count()
temp
customer_id product_type
472848 clothes 1
479485 bags 1
clothes 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.