0

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.

not_speshal
  • 22,093
  • 2
  • 15
  • 30
nammerkage
  • 232
  • 2
  • 17
  • 1
    Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Aug 23 '23 at 14:00

2 Answers2

2

You could join on just the row number by resetting the appropriate index level:

>>> df.join(transformed.rename("rolling_sum").reset_index(level=0, drop=True))

   bank  account_money  rolling_sum
0     1            100        100.0
1     1            300        400.0
2     2            500        500.0
not_speshal
  • 22,093
  • 2
  • 15
  • 30
1

You can do it by executing this code using .reset_index:

df['window_sum'] = transformed.reset_index(level=0, drop=True)

   bank  account_money window_sum
0     1            100      100.0
1     1            300      400.0
2     2            500      500.0
dominikfey
  • 108
  • 6