熊猫基于相互依赖的滞后值计算列

时间:2018-07-18 18:00:42

标签: list pandas numpy for-loop

我有一个如下所示的数据框。最右边的两列是我想要的列:

Open    Close   open_to_close   close_to_next_open  open_desired    close_desired
0          0       0                  3             0                  0
0          0       4                  8             3                  7
0          0       1                  1            15                 16

计算如下:

open_desired = close_desired(prior row) + close_to_next_open(prior row)
close_desired = open_desired + open_to_close

如何以循环方式实现以下内容?我正在尝试直到最后一行。

 df = pd.DataFrame({'open': [0,0,0], 'close': [0,0,0], 'open_to_close': [0,4,1], 'close_to_next_open': [3,8,1]}) 
 df['close_desired'] = 0 
 df['open_desired'] = 0
 ##First step is to create open_desired in current row which is dependent on close_desired in previous row
 df['open_desired'] = df['close_desired'].shift() + df['close_to_next_open'].shift()
 ##second step is to create close_desired in current row which is dependent on open_desired in current row
 df['close_desired'] = df['open_desired'] + df['open_to_close']
 df.fillna(0,inplace=True)

1 个答案:

答案 0 :(得分:1)

我唯一想到的方法是使用iterrows()

for row, v in df.iterrows():
    if row>0:
        df.loc[row,'open_desired'] = df.shift(1).loc[row, 'close_desired'] + df.shift(1).loc[row, 'close_to_next_open']
        df.loc[row,'close_desired'] = df.loc[row, 'open_desired'] + df.loc[row, 'open_to_close']