当数据库是第二个位置参数时,熊猫应用函数

问题描述 投票:0回答:1

假设我有一个功能

def fun(x, y):
    return(y.sum() + x)

此函数输入一个值以添加x,并添加一个数据库/序列以求和y

如果我有数据库df,我想将其功能应用到其中。但是,数据库应该是第二个位置参数,而不是第一个。

df.apply(fun, x = 10)

返回错误“(” fun()获得了参数'x'“的多个值,'发生在索引0')

如何使用apply函数使数据库成为second位置参数?

python pandas dataframe apply series
1个回答
0
投票

如果不允许您修改函数fun,则需要在apply内附加如下所示的lambda:

df.apply(lambda col: fun(10, col))
© www.soinside.com 2019 - 2024. All rights reserved.