Good Morning,
I have the following dataframe:
a = [1,2,3,4,5,6]
b = pd.DataFrame({'a': a})
I would like to create a column that sums the next "n" rows of column "a", including the present value of a; I tried:
n = 2
b["r"] = pd.rolling_sum(b.a, n) + a
print(b)
a r
0 1 NaN
1 2 5.0
2 3 8.0
3 4 11.0
4 5 14.0
5 6 17.0
It would be delightful to have:
a r
0 1 1 + 2 + 3 = 6
1 2 2 + 3 + 4 = 9
2 3 3 + 4 + 5 = 12
3 4 4 + 5 + 6 = 15
4 5 5 + 6 + 0 = 11
5 6 6 + 0 + 0 = 6