I want to apply a function with arguments to a series in python pandas:
x = my_series.apply(my_function, more_arguments_1)
y = my_series.apply(my_function, more_arguments_2)
...
The documentation describes support for an apply method, but it doesn't accept any arguments. Is there a different method that accepts arguments? Alternatively, am I missing a simple workaround?
Update (October 2017): Note that since this question was originally asked that pandas apply()
has been updated to handle positional and keyword arguments and the documentation link above now reflects that and shows how to include either type of argument.
Newer versions of pandas do allow you to pass extra arguments (see the new documentation). So now you can do:
my_series.apply(your_function, args=(2,3,4), extra_kw=1)
The positional arguments are added after the element of the series.
For older version of pandas:
The documentation explains this clearly. The apply method accepts a python function which should have a single parameter. If you want to pass more parameters you should use functools.partial
as suggested by Joel Cornett in his comment.
An example:
>>> import functools
>>> import operator
>>> add_3 = functools.partial(operator.add,3)
>>> add_3(2)
5
>>> add_3(7)
10
You can also pass keyword arguments using partial
.
Another way would be to create a lambda:
my_series.apply((lambda x: your_func(a,b,c,d,...,x)))
But I think using partial
is better.