Используйте Series.rolling
с параметром min_periods=1
и sum
:
df = pd.DataFrame({'feature':[1,2,4,5,6,2,3,4,5]})
df['new_feature'] = df['feature'].rolling(5, min_periods=1).sum()
print (df)
feature new_feature
0 1 1.0
1 2 3.0
2 4 7.0
3 5 12.0
4 6 18.0
5 2 19.0
6 3 20.0
7 4 20.0
8 5 20.0