[英]How to fill a column with the sum of another column and the previous value of the same column?
I am building a financial model in Python.我正在用 Python 构建一个财务模型。 To do so, I need to calculate the "tax carry forward".为此,我需要计算“税收结转”。 This position consists of the "EBT" plus the "tax carry forward" of the previous period.该头寸由“EBT”加上上一期间的“税收结转”组成。 In the first period, the "tax carry forward" is equal to "EBT".在第一期,“税收结转”等于“EBT”。
I am currently trying to solve this with the df.shift() function:我目前正在尝试使用 df.shift() 函数解决这个问题:
df2["carry forward"] = df2["EBT"] + df2["carry forward"].shift(periods=1, fill_value=0)
This, however, doesn't work properly.但是,这不能正常工作。 While I get correct results for the first two iterations (2021, 2022), it doesn't work anymore from the third iteration onwards.虽然我在前两次迭代(2021 年、2022 年)中得到了正确的结果,但从第三次迭代开始就不再起作用了。
EBT carry forward year 2021 -377893.353711 -377893.353711 2022 -282754.978037 -660648.331748 2023 -224512.990469 -507267.968506 2024 -167696.637680 -392209.628149
The carry forward, as shown in the table above, for the year 2023 is the sum of EBT 2023 and 2022, which is incorrect.如上表所示,2023 年的结转是 EBT 2023 和 2022 的总和,这是不正确的。 I can't quite figure out my mistake, because I am not sure how exactly Python is populating columns in a dataframe.我不太清楚我的错误,因为我不确定 Python 是如何在数据框中填充列的。 To me, it looks like Python isn't populating the dataframe row by row, but rather simultaneously.对我来说,看起来 Python 不是逐行填充数据帧,而是同时填充。 Is this the problem?这是问题吗? If so, how do I work around it?如果是这样,我该如何解决? Is there a better way to do the task than the df.shift() function?有没有比 df.shift() 函数更好的方法来完成任务?
I assume that you are actually looking for a cumulative sum of the column EBT
for the column of carry_forward
?我假设您实际上是在寻找carry_forward
列的EBT
列的累积总和?
For the input of:对于输入:
EBT carry_forward
year
2021 -377893.353711 0
2022 -282754.978037 0
2023 -224512.990469 0
2024 -167696.637680 0
with:和:
df["carry_forward"] = df["EBT"].cumsum()
df
You will get:你会得到:
EBT carry_forward
year
2021 -377893.353711 -3.778934e+05
2022 -282754.978037 -6.606483e+05
2023 -224512.990469 -8.851613e+05
2024 -167696.637680 -1.052858e+06
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.