【发布时间】:2020-08-09 21:52:23
【问题描述】:
数据框:-
ID spend month_diff
12 10 -1
12 10 -2
12 20 1
12 30 2
13 15 -1
13 20 -2
13 25 1
13 30 2
我想根据特定 ID 的月差来获取支出总计。负数表示客户去年的支出,正数表示今年。所以,我想比较客户过去一年和今年的支出。所以条件如下:
条件:-
if month_diff >= -2 and < 0 then cumulative spend for negative months - flag=pre
if month_diff > 0 and <=2 then cumulative spend for positive months - flag=post
所需的数据框:-
ID spend month_diff tot_spend flag
12 10 -2 20 pre
12 30 2 50 post
13 20 -2 35 pre
13 30 2 55 post
【问题讨论】: