Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a function with multiple arguments to DataFrame.apply

Suppose I have a dataframe like this:

df = pd.DataFrame([['foo', 'x'], ['bar', 'y']], columns=['A', 'B'])


       A    B
0    foo    x
1    bar    y

I know how to use a single argument function with Apply when it comes to dataframes, like this:

def some_func(row):
    return '{0}-{1}'.format(row['A'], row['B'])

df['C'] = df.apply(some_func, axis=1)

df


       A    B        C
0    foo    x    foo-x
1    bar    y    bar-y

How can I use apply on dataframes when they involve multiple input arguments? Here's an example of what I want:

def some_func(row, var1):
    return '{0}-{1}-{2}'.format(row['A'], row['B'], var1)

df['C'] = df.apply(some_func(row, var1='DOG'), axis=1)

df


       A    B            C
0    foo    x    foo-x-DOG
1    bar    y    bar-y-DOG

I'm not looking for work-arounds to solve this one particular example, just how to do something like this in general. Any advice would be well appreciated, thanks.

like image 264
Michael Henry Avatar asked Mar 12 '18 14:03

Michael Henry


People also ask

How do you pass arguments to a function in Python?

Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

What is passed to the apply function?

The apply() function is used to apply a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame's index (axis=0) or the DataFrame's columns (axis=1).

What is the use of apply function in pandas?

The apply() method allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.


1 Answers

It's just the way you think it would be, apply accepts args and kwargs and passes them directly to some_func.

df.apply(some_func, var1='DOG', axis=1)

Or,

df.apply(some_func, args=('DOG', ), axis=1)

0    foo-x-DOG
1    bar-y-DOG
dtype: object
like image 75
cs95 Avatar answered Sep 19 '22 16:09

cs95