How can I use `apply` with a function that takes multiple inputs

user3600497 picture user3600497 · Oct 9, 2015 · Viewed 7.5k times · Source

I have a function that has multiple inputs, and would like to use SFrame.apply to create a new column. I can't find a way to pass two arguments into SFrame.apply.

Ideally, it would take the entry in the column as the first argument, and I would pass in a second argument. Intuitively something like...

def f(arg_1,arg_2):
    return arg_1 + arg_2

sf['new_col'] = sf.apply(f,arg_2)

Answer

Yuan Huang picture Yuan Huang · Oct 14, 2015

suppose the first argument of function f is one of the column.

Say argcolumn1 in sf, then

sf['new_col'] = sf['argcolumn1'].apply(lambda x:f(x,arg_2))

should work