python - 从上一行/值 python 添加值

标签 python pandas numpy sum add

是否可以像这样用之前在 Python 中定义的数字的值从每一行添加值:

base_value = 10

a b c
2 3 (expected. 2+3+base_value=10)
4 3 (expected. 4+3+15=22)
1 9 (expected. 1+9+22=32)
5 7 (expected. 5+7+32=44)
1 1 (expected. 1+1+44=46)
... 

提前致谢

最佳答案

使用Series.addSeries.cumsum :

base_value = 10
df['c'] = df['a'].add(df['b']).cumsum().add(base_value)

print (df)
   a  b   c
0  2  3  15
1  4  3  22
2  1  9  32
3  5  7  44
4  1  1  46

关于python - 从上一行/值 python 添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65698406/

相关文章:

python - 跳过枚举中的前两个元素——python

python - 为什么 ffmpeg 警告 "Guessed Channel Layout for Input Stream #0.0 : mono"?

python - 追加和读取相同的文件 - Python

python - groupby 并根据另一列的值保留一列的信息

python - MATLAB 上的索引矩阵行和列,这段代码可以吗?

python - 有效地生成大量有偏差的抛硬币

python - 使用 Python Elasticserarch-py 包时出错

python - 获取列中的最大值

python - 通过索引/列元素连接数据帧

python - 为什么当我使用 .astype(str) 时 numpy/pandas 仅返回第一个字符