Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 842
user3600497 Avatar asked Oct 09 '15 01:10

user3600497


People also ask

How can you accept multiple arguments into a function?

The * symbol is used to pass a variable number of arguments to a function. Typically, this syntax is used to avoid the code failing when we don't know how many arguments will be sent to the function.

How do I use multiple functions in R?

The lapply () and sapply () functions can be used for performing multiple functions on a list in R. This function is used in order to avoid the usage of loops in R. The difference between both the functions is the sapply () function does the same job as lapply () function but returns a vector.

What does Mapply do in R?

The mapply() function in R is used to apply a function FUN to a given list or a vector argument passed to it.

What are input parameters in Python?

The parameter to input() is a prompt string that prints out, prompting the user what they are supposed to type. It looks best to add a space at the end of the prompt string.


2 Answers

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

like image 55
Yuan Huang Avatar answered Oct 02 '22 19:10

Yuan Huang


Try this.

sf['new_col'] = sf.apply(lambda x : f(arg_1, arg_2))
like image 39
druk Avatar answered Oct 02 '22 19:10

druk