我正在尝试添加一个“每月订单”列,该列计算具有特定id的客户在特定的CohortDate中有多少笔交易
基本上,它是一个COUNTIFS函数,其中范围是所有ID和所有CohortDates等于任何给定行的ID和CohortDate
非常感谢您的帮助
import pandas as pd
import numpy as np
df = pd.DataFrame({'order_id': [75054,75057,75059,75061,75066],
'customer_id': [101692,101694,101734,101692,101694],
'CohortDate': ['2016-05','2016-05','2016-05','2016-05','2016-06']
})
我希望得到的结果如下:
order_id customer_id CohortDate Monthly_orders
75054 101692 '2016-05' 2
75057 101694 '2016-05' 1
75059 101734 '2016-05' 1
75061 101692 '2016-05' 2
75066 101694 '2016-06' 1
为了按某些变量分组,我们可以使用
transform
,它将groupby应用于一个系列,而不是返回一个新的数据帧这将返回原始数据帧的计数
相关问题 更多 >
编程相关推荐