Search code examples
pythonapplykeyword-argument

Passing keyword arguments within apply causes invalid syntax error


I am getting a syntax error when using **kwargs within the apply function:

def lookup_price(x,y, **kwargs):
    var1=x+y
    if IWM==True:
       var1=x-y
return var1 

This is how I am using it:

 df['col1'] = df.apply(lookup_price, axis='columns', args=(1,2,IWM=True,)) 

I am getting this error:

SyntaxError: invalid syntax

Can anyone see what I am doing wrong?


Solution

  • Check the syntax of the df.apply(). Click Here You should not pass keyword args as a parameter in a tuple. It will be passed as an attribute-value pair.

    Something like this:

    df['col1'] = df.apply(lookup_price, axis='columns', args=(1,2,), IWM=True)