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

编程入门 行业动态 更新时间:2024-10-12 05:48:37
本文介绍了将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作为参数

本文发布于:2023-10-19 17:56:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   Dataframe   Apply   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!