I have some data in a made up example. It's a bank with a few accounts:
d = {'bank': [1, 1, 2], 'account_money': [100, 300, 500]}
df = pd.DataFrame(data=d)
>> df
bank account_money
0 1 100
1 1 300
2 2 500
For some reason, we want to do a rolling window sum:
def transform(grp_obj):
result = grp_obj['account_money'].rolling(2, min_periods=0).sum()
return result
transformed = df.groupby('bank').pipe(transform)
>>> transformed
bank
1 0 100.0
1 400.0
2 2 500.0
Name: account_money, dtype: float64
How can I join the result back to the original df? Preferably with df.join
?
It should be possible since we have the bank ID, the original row number and then the desired window_sum.