简体   繁体   English

将Dataframe传递给Apply函数pandas作为参数

[英]Pass Dataframe to Apply function pandas as argument

Is possible to pass a DataFrame to an apply function like this? 是否可以将DataFrame传递给这样的apply函数?

df2 = df1.apply(func,axis=1,args=df2)
def func(df1,df2):
    # do stuff in df2 for each row of df1
    return df2

The two DataFrames do not have the same length. 两个数据帧的长度不同。

From the df.apply docs: df.apply文档中:

DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)

Applies function along input axis of DataFrame. 沿DataFrame的输入轴应用功能。

args : tuple argstuple

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. 如果知道您要实现的目标,则可以对代码进行进一步的改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM