将数据帧传递给应用函数 pandas 作为参数 [英] Pass Dataframe to Apply function pandas as argument
本文介绍了将数据帧传递给应用函数 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
文档:
From the df.apply
docs:
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.
这篇关于将数据帧传递给应用函数 pandas 作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文