Duudsrednaz Duudsrednaz - 1 year ago 62
Python Question

Pandas Dataframe.apply() raises typeerror for providing too many arguments

I have this function:

def function(row,args):
array = np.array(row['Revenue'+args[0]:'Revenue'+str(args[1])]).astype(float)
if np.mean(array) < (5000/12):
return 'XXS'
if np.mean(array) < (10000/12):
return 'XS'
if np.mean(array) < (25000/12):
return 'S'
if np.mean(array) < (50000/12):
return 'M'
if np.mean(array) < (250000/12):
return 'L'
if np.mean(array) < (750000/12):
return 'XL'
if np.mean(array) >= (750000/12):
return 'XXL'


I want to apply this function using Pandas.Dataframe.apply(). So I use args, since I need to pass two additional arguments.

df.apply(function,axis=1,args=(VARIABLE1,VARIABLE2))


Somehow I get the error:

TypeError: ('klantschaling() takes 2 positional arguments but 3 were given')


I'm obviously giving two arguments: the dataframe row and args. So why do I get the error?

Answer Source

You have two options. Either you can modify your function signature, or you can pass a tuple in df.apply.

The first option would be to redefine your function like this:

def function(row, arg1, arg2):
    ...

And then call df.apply as you are. The second, simpler alternative is to wrap your arguments in an iterable:

df.apply(function, axis=1, args=([VARIABLE1, VARIABLE2], ))

Or,

df.apply(function, axis=1, args=((VARIABLE1, VARIABLE2), ) )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download