Passing a function with multiple arguments to DataFrame.apply
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
You should use vectorized logic:
df['C'] = df['A'] + '-' + df['B'] + '-DOG'
If you really want to use df.apply
, which is just a thinly veiled loop, you can simply feed your arguments as additional parameters:
def some_func(row, var1):
return '{0}-{1}-{2}'.format(row['A'], row['B'], var1)
df['C'] = df.apply(some_func, var1='DOG', axis=1)
As per the docs, df.apply
accepts both positional and keyword arguments.
I think it can be
df.apply('-'.join,1)+'-DOG'
Out[157]:
0 foo-x-DOG
1 bar-y-DOG
dtype: object