8

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)
user3600497
  • 1,621
  • 1
  • 18
  • 22

3 Answers3

21

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

Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
Yuan Huang
  • 311
  • 1
  • 4
  • please replace f with your function name like if you have function named count_imp_words then you will write: products['awesome'] = products['word_count'].apply(lambda x:count_imp_words(x,'awesome')) – Rajesh Goel Feb 04 '17 at 01:38
2

Try this.

sf['new_col'] = sf.apply(lambda x : f(arg_1, arg_2))
druk
  • 553
  • 6
  • 16
0

The way i understand your question (and because none of the previous answers are marked as accepted), it seems to me that you are trying to apply a transformation using two different columns of a single SFrame, so:

As specified in the online documentation, the function you pass to the SFrame.apply method will be called for every row in the SFrame.

So you should rewrite your function to receive a single argument representing the current row, as follow:

def f(row):
    return row['column_1'] + row['column_2']

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