Pandas apply function when database is the 2nd positional argument

Viewed 157

Lets say I have a function

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

This function takes in a value to add x, and a database/series to sum y

If I have a database df, that I would like to apply my function to. However, the data base is meant to be the second positional argument not the first.

df.apply(fun, x = 10)

returns an error "("fun() got multiple values for argument 'x'", 'occurred at index 0')

How do I use the apply function so that the database is the second positional argument?

1 Answers

If you are not allowed to modify function fun, you need additional lambda inside apply as follows:

df.apply(lambda col: fun(10, col))
Related