是否可以像这样用之前在 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.add
与 Series.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/