gpt4 book ai didi

Python:在两列之间创建某种累积和

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:24 24 4
gpt4 key购买 nike

我试图弄清楚如何使用多列获得某种运行总计,但我什至不知道从哪里开始。我以前使用过 cumsum,但只用于一列,这不起作用。

我有这张表:

      Index       A         B       C    
1 10 12 20
2 10 14 20
3 10 6 20

我正在尝试构建如下所示的表格:

      Index       A         B       C       D
1 10 12 20 10
2 10 14 20 18
3 10 6 20 24

D的计算公式如下:D2 = ( D1 - B1 ) + C1

D1 = A 列

关于如何做到这一点有什么想法吗?我对此完全没有想法。

最佳答案

这应该有效:


df.loc[0, 'New_Inventory'] = df.loc[0, 'Inventory']
for i in range(1, len(df)):
df.loc[i, 'New_Inventory'] = df.loc[i-1, 'Inventory'] - df.loc[i-1, 'Booked'] - abs(df.loc[i-1, 'New_Inventory'])
df.New_Inventory = df.New_Inventory.astype(int)

df
# Index Inventory Booked New_Inventory
#0 1/1/2020 10 12 10
#1 1/2/2020 10 14 -12
#2 1/3/2020 10 6 -16

关于Python:在两列之间创建某种累积和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59001498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com