1 回答
TA贡献1798条经验 获得超3个赞
我认为这只是几天的滚动:
def get_roll(x):
s = pd.Series(x['amount'].values,
index=pd.to_datetime('1900-01-01') + pd.to_timedelta(x['day'], unit='D')
)
return pd.Series(s.rolling('2D').sum().values, index=x.index)
df['amount2days'] = (df.groupby('customer').apply(get_roll)
.reset_index(level=0, drop=True)
)
输出:
day customer amount amount2days
1 1 cust1 500 500.0
2 1 cust2 100 100.0
3 1 cust1 50 550.0
4 2 cust1 100 650.0
5 2 cust2 250 350.0
6 3 cust1 20 120.0
选项 2:由于您只想计算两天的累计金额,因此今天的金额仅加上前一天的金额。所以我们可以利用shift:
df['amount2days'] = df.groupby(['customer','day'])['amount'].cumsum()
# shift the last item of the previous day and add
df['amount2days'] += (df.drop_duplicates(['day','customer'],keep='last')
.groupby(['customer'])['amount2days'].shift()
.reindex(df.index)
.ffill()
.fillna(0)
)
添加回答
举报