-2

How do I change the data so that every single entry in some pandas df column will now show the sum of the last say 4 entries? The first 3 entries can just stay as the cumulative sum, however after the first 3 I want each entry to have a value that was the sum of the last 4 in the original column.

E.g. 1, 2, 3, 4, 5, 2 would become 1, 3 (2+1) 6 (3+2+1), 10 (4+3+2+1), 14 (5+4+3+2), 14 (2+5+4+3)

Thank you.

gauzah
  • 15
  • 1
  • 3
  • 3
    Please read the following documentation, then [edit] and rephrase the question. [Take the Tour](https://stackoverflow.com/tour) & [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Always provide a [mre] **with code, data, errors, current output, and expected output, as text** & you're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592). – Trenton McKinney Aug 27 '20 at 22:34
  • 1
    See [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard()`](https://stackoverflow.com/questions/52413246) – Trenton McKinney Aug 27 '20 at 22:35
  • maybe you should use `rolling` (moving window) with window's size `4` ? and then you can use `sum()` – furas Aug 27 '20 at 22:42
  • Pandas `cumsum()` method also comes to mind – G. Anderson Aug 27 '20 at 22:46

1 Answers1

2

You can use rolling() (moving window) with size 4 and minimal size 1 and then use sum()

import pandas as pd

df = pd.DataFrame({"A":[1, 2, 3, 4, 5, 2]})
df['B'] = df.rolling(4, min_periods=1).sum()

print(df)

Result

   A     B
0  1   1.0
1  2   3.0
2  3   6.0
3  4  10.0
4  5  14.0
5  2  14.0
furas
  • 134,197
  • 12
  • 106
  • 148