是否可以像这样将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 文档:
DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)
DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)
沿输入轴应用功能 数据框.
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作为参数
发布评论