如何将前一行的结果添加到当前行的内容? [英] How to add result of previous row to contents of present row?
问题描述
import pandas as pd
import numpy as np
df = pd.DataFrame({"a": [7, 2, 3], "b": [4, 5, 6], "c": [100, np.nan, np.NaN]})
df
Out[11]:
a b c
0 7 4 100.0
1 2 5 NaN
2 3 6 NaN
对于上面的Python pandas DataFrame,我想对第0行进行操作:
计算列a
和b
之间的差.结果应添加到列c
的内容中,并存储在(新)列d
中.
For the above Python pandas DataFrame, I would like to do for row number zero:
calculate the difference between column a
and b
. The result of this should be added to the contents of column c
and stored in a (new) column d
.
对于第一个行,第一步,应将第零行/列d
的内容存储在列c
中.
此后,应该应用与之前针对第零行相同的算法.
For row number one, in the first step, the contents of row zero / column d
should be stored in column c
.
Afterwards the same algorithm like before for row zero should be applied.
执行完此操作后,所得的DataFrame将如下所示:
After performing this, the resulting DataFrame would look like:
a b c d
0 7 4 100.0 103.
1 2 5 103.0 100.
2 3 6 100.0 97.
实际上,数据框具有比此小示例更多的行.因此,快速的计算速度非常重要.
In reality, the dataframe has much more rows than this small example. Therefore a fast computational speed is quite important.
用于计算此新数据框的解决方案会是什么样子?
How would a solution for computing this new dataframe look like?
推荐答案
您可以获取a
和b
之间的差异的累积和,并将其添加到列c
的初始值中,然后填充其余部分新计算的d
中c
的值,向下移动1:
You can get the cumulative sum of the difference between a
and b
, add that to your column c
initial value, and populate the rest of c
with your newly calculated d
, shifted down by 1:
df['d'] = df.a.sub(df.b).cumsum().add(df.c.iloc[0])
df.loc[1:,'c'] = df.d.shift()
>>> df
a b c d
0 7 4 100.0 103.0
1 2 5 103.0 100.0
2 3 6 100.0 97.0
这篇关于如何将前一行的结果添加到当前行的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!