简体   繁体   English

如何将上一行的值传递给 dataframe 应用 function?

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

相关问题 如何根据行值将 function 应用于 dataframe - How to apply function to dataframe based on row value 将 function 应用于包含上一行的 dataframe - Apply a function to a dataframe which includes the previous row 将 function 应用到引用前一行数据的 dataframe 行 - Apply function to dataframe row which references previous row's data A pandas dataframe 列作为行级 function 的参数传递,以将列的每个值应用于其各自的行 - A pandas dataframe column to pass as an argument of row level function to apply each value of the column to its respective row Pandas 中是否有一种方法可以在 dataframe.apply 中使用先前的行值,而先前的值也在应用程序中计算? - Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply? 将 function 应用于 dataframe,其中包括前一行数据 - Apply a function to dataframe which includes previous row data Pandas 数据框:将函数应用于前一行的行值和值 - Pandas dataframe : Applying function to row value and value from the previous row 如何使用python apply/lambda/shift函数根据2列的值获取该特定列的前一行值? - How to use python apply/lambda/shift function to get the previous row value of that particular column based on the value of 2 columns? 如何在 dataframe 的每一行上应用 function? - How to apply a function on every row on a dataframe? 如何将我的 function 应用到 dataframe 的第一行? - How to apply my function to the first row of a dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM