Home > Software engineering >  Lambda with two values does not work, error: <lambda>() missing 1 required positional argument
Lambda with two values does not work, error: <lambda>() missing 1 required positional argument

Time:06-02

I have an dataframe:

data = {'key': ['one', 'two', 'zero'], 'cost': [300, 20, 500], 'leads': [20, 0, 50]} 
    data = pd.DataFrame(data=data)

I want to calculate the new value through a function that looks like this:

def cpl_calc(x, y):
    if x == 0:
        cpl = y
    else: 
        cpl = y / x
    print(cpl)

When I try to count like this:

data['cpl'] = data[['cost', 'leads']].apply(lambda x, y: cpl_calc(x, y))

I see the following error TypeError: () missing 1 required positional argument: 'y'

How can I make the formula calculate the new value correctly?

CodePudding user response:

As @Corralien states, you are better off to vectorize this. See that answer. But to understand how to do this via an apply() function you could:

def cpl_calc(x):
    if x['cost'] == 0:
        cpl = x['leads']
    else: 
        cpl = x['leads'] / x['cost']
     
    return cpl

data.apply(cpl_calc, axis=1)

0    0.066667
1    0.000000
2    0.100000

CodePudding user response:

In your case, you can use vectorization:

data['cost'].where(data['leads'] == 0, other=data['leads'] / data['cost'])
print(data)

# Output
    key  cost  leads        cpl
0   one   300     20   0.066667
1   two    20      0  20.000000
2  zero   500     50   0.100000
  • Related