python pandas: apply a function with arguments to a series python pandas: apply a function with arguments to a series python python

python pandas: apply a function with arguments to a series


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.


Steps:

  1. Create a dataframe
  2. Create a function
  3. Use the named arguments of the function in the apply statement.

Example

x=pd.DataFrame([1,2,3,4])  def add(i1, i2):      return i1+i2x.apply(add,i2=9)

The outcome of this example is that each number in the dataframe will be added to the number 9.

    00  101  112  123  13

Explanation:

The "add" function has two parameters: i1, i2. The first parameter is going to be the value in data frame and the second is whatever we pass to the "apply" function. In this case, we are passing "9" to the apply function using the keyword argument "i2".


Series.apply(func, convert_dtype=True, args=(), **kwds)args : tuplex = my_series.apply(my_function, args = (arg1,))