使用pandas dataframes,我试图根据CustId获取每行(不包括当前行本身)在过去90天内的平均购买数量,然后添加一个新列“PurchaseMeansLast90Days”
这是我尝试的代码,不正确:
group = df.groupby(['CustId'])
df['PurchaseMeanLast90Days'] = group.apply(lambda g: g[g['Date'] > (pd.DatetimeIndex(g['Date']) + pd.DateOffset(-90))])['Purchases'].mean()
以下是我的数据:
例如,行索引5将这些行包含在它的mean()=3.33中
新的数据帧将如下所示(我没有对CustId=2进行计算):
您可以执行滚动计算:
印刷品:
相关问题 更多 >
编程相关推荐