How can I use apply with a function that accepts multiple inputs

I have a function with multiple inputs and would like to use SFrame.apply to create a new column. I cannot find a way to pass two arguments to SFrame.apply .

Ideally, a column will require a record as the first argument, and I will pass the second argument. Intuitively something like ...

 def f(arg_1,arg_2): return arg_1 + arg_2 sf['new_col'] = sf.apply(f,arg_2) 
+8
sframe graphlab
source share
3 answers

Suppose that the first argument of f is one of the columns.

Say argcolumn1 in sf, then

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

must work

+16
source share

Try it.

 sf['new_col'] = sf.apply(lambda x : f(arg_1, arg_2)) 
+1
source share

As I understand your question (and since none of the previous answers are marked as accepted), it seems to me that you are trying to apply the transformation using two different columns of the same SFrame , therefore:

As indicated in the online documentation , the function that you pass to the SFrame.apply method will be called for each line in the SFrame.

Therefore, you must rewrite your function to get one argument representing the current line, as shown below:

 def f(row): return row['column_1'] + row['column_2'] sf['new_col'] = sf.apply(f) 
0
source share

All Articles