简体   繁体   English

如何用另一列的总和与同一列的前一个值填充一列?

[英]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.

相关问题 如何将列的实际行与另一列熊猫的前一行相加? - How to sum actual row of column to previous row of another column pandas? 如何使用Python获取另一列中具有相同值的值的总和? - How to get the sum of values with the same value in another column with Python? 用上一年同月的值填充列 - Fill column with value from previous year from the same month 如何用同一列中的值填充 null 列中的 Pyspark Dataframe 值,其在另一列中的对应值相同 - How to fill null values in a Pyspark Dataframe column with values from the same column, whose corresponding value in another column is same 如何根据同一行中另一列中的值向前填充列值 - how to forward fill a column values based on the value in another column in same row 通过对一列进行分组并对另一列中的先前值求和来创建数据框列 - Create dataframe column by grouping a column and sum previous values in another column 如何根据另一列填充 nan 值 - how to fill nan value based on another column 如果另一列的值匹配,如何在列中填充 nan 值 - How to fill nan values in a column if the value from another column matches 如何根据计算的同一列中的先前值计算 pandas 列? - How to calculate a pandas column based on the previous value in the same column that is calculated? 如何将带有数字的列更改为反映与前一行列值按另一列值分组相关的变化的列? - How to change column with numbers to column reflecting change in relation to previous row column value grouping by another column value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM