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

If for any reason that won’t work for your use case, then you can always fallback to using a lambda:

df.apply(lambda row: some_func(row, 'DOG'), axis=1)

0    foo-x-DOG
1    bar-y-DOG
dtype: object

Leave a Comment

techhipbettruvabetnorabahisbahis forumu