[英]How to pass the value of previous row to the dataframe apply function?
I have the following pandas dataframe and would like to build a new column 'c' which is the summation of column 'b' value and column 'a' previous values.我有以下 pandas dataframe 并想构建一个新列“c”,它是列“b”值和列“a”先前值的总和。 With shifting column 'a' it is possible to do so.通过移动“a”列,可以这样做。 However, I would like to know how I can pass the previous values of column 'a' in the apply()
function.但是,我想知道如何在apply()
function 中传递“a”列的先前值。
l1 = [1,2,3,4,5]
l2 = [3,2,5,4,6]
df = pd.DataFrame(data=l1, columns=['a'])
df['b'] = l2
df['shifted'] = df['a'].shift(1)
df['c'] = df.apply(lambda row: row['shifted']+ row['b'], axis=1)
print(df)
a b shifted c
0 1 3 NaN NaN
1 2 2 1.0 3.0
2 3 5 2.0 7.0
3 4 4 3.0 7.0
4 5 6 4.0 10.0
I appreciate your help.我感谢您的帮助。
Edit: this is a dummy example.编辑:这是一个虚拟的例子。 I need to use the apply function because I'm passing another function to it which uses previous rows of some columns and checks some condition.我需要使用 apply function 因为我将另一个 function 传递给它,它使用某些列的前几行并检查某些条件。
First let's make it clear that you do not need apply
for this simple operation, so I'll consider it as a dummy example of a complex function.首先让我们明确一点,您不需要apply
这个简单的操作,因此我将其视为复杂 function 的虚拟示例。
Assuming non-duplicate indices, you can generate a shifted Series and reference it in apply
using the name
attribute:假设非重复索引,您可以生成一个移位系列并使用name
属性在apply
中引用它:
s = df['a'].shift(1)
df['c'] =df.apply(lambda row: row['b']+s[row.name], axis=1)
output: output:
a b shifted c
0 1 3 NaN NaN
1 2 2 1.0 3.0
2 3 5 2.0 7.0
3 4 4 3.0 7.0
4 5 6 4.0 10.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.