Why the function apply's args can only space the later site?

Viewed 17

Question1

I'm trying to let a dataframe columns add the same number.

Trying use apply to make columns add a number.

First define a function call 'apply_age'

def apply_age(x,bias):
    return x+bias

data["age"] = data["age"].apply(apply_age,args=(50,))
#it can work

but

def apply_age(x,bias):
    return x+bias

data["age"] = data["age"].apply(apply_age,args=(,50))
#it can't work,and show SyntaxError: invalid syntax

Question2

Why type 2 number in each site will get error ?

Which is 'TypeError: apply_age() takes 2 positional arguments but 3 were given def apply_age(x,bias)'

data["age"] = data["age"].apply(apply_age,args=(50,2))
1 Answers

Can you just use a lambda function instead?

bias = 50
df['age'] = df['age'].apply(lambda x: x+bias)
Related