使用前一行进行迭代

asd*_*asd 3 python python-3.x pandas

如何使用迭代类型的规则创建 col2?

col2 = col1 + 0.1(col2's previous value). If there is no previous value (here in 20 Dec 2019), then col2 should equal col1

df
  date      col1   
2019-12-20   10      
2019-12-27   3       
2020-01-03   7

Run Code Online (Sandbox Code Playgroud)

预期产出

  date      col1  col2  
2019-12-20   10     10   (no previous value, so equal col1)
2019-12-27   3      4    (3+0.1*10)
2020-01-03   7      7.4  (7+0.1*4)
Run Code Online (Sandbox Code Playgroud)

Cor*_*ien 6

使用np.cumsum (灵感来自@MustafaAyd?n 的公式)

p = 0.1 ** np.arange(len(df)-1, -1, -1)
df['col2'] = np.cumsum(p * df['col1']) / p
Run Code Online (Sandbox Code Playgroud)
>>> df
         date  col1  col2
0  2019-12-20    10  10.0
1  2019-12-27     3   4.0
2  2020-01-03     7   7.4
Run Code Online (Sandbox Code Playgroud)