Python 3.x 将递归函数应用于dataframe列
我想根据另一列的函数和当前行之前的行生成一个新列 即Python 3.x 将递归函数应用于dataframe列,python-3.x,pandas,dataframe,recursion,Python 3.x,Pandas,Dataframe,Recursion,我想根据另一列的函数和当前行之前的行生成一个新列 即 NewColumn{n}=OldColumn{n}*0.6+NewColumn{n-1}*0.4 我是否可以不用iterrows来执行此操作 我看到这个帖子: 但我想知道熊猫是否还有其他功能/包或技巧 类似于R?中的purrr,您可以在此处编写自定义函数并使用 def func(s): # s is `Series` newColumn = [0] for i, val in enumerate(s): ne
NewColumn{n}=OldColumn{n}*0.6+NewColumn{n-1}*0.4
我是否可以不用iterrows
来执行此操作
我看到这个帖子:
但我想知道熊猫是否还有其他功能/包或技巧
类似于R?中的
purrr
,您可以在此处编写自定义函数并使用
def func(s): # s is `Series`
newColumn = [0]
for i, val in enumerate(s):
newColumn.append(val*0.6 + newColumn[i]*0.4)
return newColumn[1:]
df['new'] = df[['old']].apply(func) #Don't use df['old'] that would call
#Series.apply which applies func element-wise
old new
0 1 0.600
1 2 1.440
2 3 2.376