将Dataframe传递给Apply函数pandas作为参数 [英] Pass Dataframe to Apply function pandas as argument

查看:753
本文介绍了将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 文档:

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作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆