Home > Enterprise >  pandas lambda with multiple arguments
pandas lambda with multiple arguments

Time:10-07

Assume there is a DataFrame such as

import pandas as pd 
import numpy as np

df = pd.DataFrame({'A':[21, 11, 31, 45],
                   'B':[10, 20, 10, 11],
                   'C':[57, 22, 34, 10]})

    A   B   C
0   21  10  57
1   11  20  22
2   31  10  34
3   45  11  10

I would like to write a function that can be used with lambda such as

def WeightedScore(x, probs):
    total = 0
    for i in range(len(probs)):
        total  = x[i]*probs[i]
    return total

df ['out'] = df.apply(lambda x: WeightedScore(x['A'], x['B'], x['C'], probs = (0.2, 0.3, 0.5)), axis = 1)

But I got an error something like this

TypeError: WeightedScore() got multiple values for argument 'probs'

Any suggestions? many thanks in advance!

CodePudding user response:

So, you are passing multiple arguments here:

df.apply(lambda x: WeightedScore(x['A'], x['B'], x['C'], probs = (0.2, 0.3, 0.5)), axis = 1)

You probably meant something like:

df.apply(lambda x: WeightedScore([x['A'], x['B'], x['C']], probs = (0.2, 0.3, 0.5)), axis = 1)

But you shouldn't be using .apply here to begin with. Instead, use vectorized operations:

df['out'] = ((0.2, 0.3, 0.5) * df).sum(axis=1)

CodePudding user response:

The issue is you are passing multiple arguments where your WeightedScore(x, probs) expects only 2, so instead pass only the x, That's why you are getting-

TypeError: WeightedScore() got multiple values for argument 'probs'

You can do it this way,

import pandas as pd
import numpy as np

df = pd.DataFrame({'A':[21, 11, 31, 45],
                   'B':[10, 20, 10, 11],
                   'C':[57, 22, 34, 10]})

def WeightedScore(x, probs):
    total = 0
    for i in range(len(probs)):
        total  = x[i]*probs[i]
    return total

df ['out'] = df.apply(lambda x: WeightedScore(x, probs = (0.2, 0.3, 0.5)), axis = 1)
print(df)

Output:

    A   B   C   out
0  21  10  57  35.7
1  11  20  22  19.2
2  31  10  34  26.2
3  45  11  10  17.3
  • Related