Reputation: 1397
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?
Upvotes: 3
Views: 2303
Reputation: 402263
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), ) )
Upvotes: 5