本文介绍了将Dataframe传递给Apply函数pandas作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以像这样将DataFrame传递给apply
函数?
Is possible to pass a DataFrame to an apply
function like this?
df2 = df1.apply(func,axis=1,args=df2)
def func(df1,df2):
# do stuff in df2 for each row of df1
return df2
两个DataFrame的长度不同.
The two DataFrames do not have the same length.
推荐答案
来自 df.apply
文档:
沿输入轴应用功能 数据框.
Applies function along input axis of DataFrame.
args
:tuple
除了要传递给函数的位置参数 数组/系列.
Positional arguments to pass to function in addition to the array/series.
正确的方法是在元组中传递参数,如下所示:
The right way is to pass your arguments in a tuple, like this:
df1.apply(func, axis=1, args=(df2, ))
如果知道您要实现的目标,则可以对代码进行进一步的改进.
Further improvements to your code would be possible if it were known what you are trying to achieve.
这篇关于将Dataframe传递给Apply函数pandas作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!