使用先前的行进行迭代

3

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

col2 = col1 + 0.1(col2的上一个值)。如果没有上一个值(例如在2019年12月20日之前),那么col2应该等于col1

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

期望输出

  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)
3个回答

7

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

p = 0.1 ** np.arange(len(df)-1, -1, -1)
df['col2'] = np.cumsum(p * df['col1']) / p

>>> 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

1
“Shift” 不能在这里使用,因为我们不是使用序列中的前一个值,而是使用迭代中的前一个值。 - Arkadiusz
1
@Arkadiusz,你是对的。我没有注意到 :-(。我已经修正了我的回答,现在应该没问题了。 - Corralien

3

通过一些数学计算,我们得到:

col2_{n} = \sum_{j=1}^{n} col1_{j} * 0.1^{n-j}

这可以使用expanding.agg来实现:

df["col2"] = (df.col1
                .expanding()
                .agg(lambda win: (0.1 ** np.arange(win.size) * win[::-1]).sum()))

其中win是传递的不断扩展的窗口,而上述公式中的n是指其大小,即win.size,而col1_{j}将成为其元素,

得到:

         date  col1  col2
0  2019-12-20    10  10.0
1  2019-12-27     3   4.0
2  2020-01-03     7   7.4

1
我使用了你的公式来修正我的答案。谢谢。+1 - Corralien

1
我只需要创建一个新的列表,然后使用col1的for循环迭代。
df = pd.DataFrame({'col1': {0: 10, 1: 3, 2: 7}})
col2 = []
for x in df.col1:
        if not len(col2):
            col2.append(x)
        else:
            col2.append(x+0.1*col2[-1])
df['col2'] = col2

很可能还有其他使用 df.shift() 的方式。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接