I have a situation where I have a dataframe row to perform calculations with, and I need to use values in following (potentially preceding) rows to do these calculations (essentially a perfect forecast based on the real data set). I get each row from an earlier df.apply
call, so I could pass the whole df along to the downstream objects, but that seems less than ideal based on the complexity of objects in my analysis.
I found one closely related question and answer [1], but the problem is actually fundamentally different in the sense that I do not need the whole df for my calcs, simply the following x
number of rows (which might matter for large dfs).
So, for example:
df = pd.DataFrame([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],
columns=['PRICE'])
horizon = 3
I need to access values in the following 3 (horizon
) rows in my row-wise df.apply
call. How can I get a naive forecast of the next 3 data points dynamically in my row-wise apply calcs? e.g. for row the first row, where the PRICE
is 100
, I need to use [200, 300, 400]
as a forecast in my calcs.
[1] apply a function to a pandas Dataframe whose returned value is based on other rows