使用其他行中的值将函数应用于 pandas 数据框行 [英] Apply function to pandas dataframe row using values in other rows
问题描述
我遇到这样一种情况,我有一个数据帧行来执行计算,并且我需要在后面的行(可能是前面的行)中使用值来进行这些计算(本质上是基于实际数据集的完美预测).我从较早的df.apply
调用中获取了每一行,因此可以将整个df传递给下游对象,但是基于分析中对象的复杂性,这似乎不太理想.
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.
我找到了一个密切相关的问答[1],但是实际上,这个问题在根本上是不同的,因为我不需要整个df来进行计算,只需下面的x
行数(这可能很重要)大型dfs).
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).
例如,
df = pd.DataFrame([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],
columns=['PRICE'])
horizon = 3
我需要在逐行df.apply
调用中访问以下3(horizon
)行中的值.如何在按行应用计算中动态地动态获取接下来3个数据点的幼稚预测?例如对于第一行,其中PRICE
是100
,我需要在计算中使用[200, 300, 400]
作为预测.
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.