1

I'm learning data analysis while performing vectorized operation with lambda function it run at first but again run it shows error as TypeError: <lambda>() takes 1 positional argument but 2 were given

sample data of tips.csv file

enter image description here

quality = lambda x:'Generous' if x['tip']/x['total_bill']>0.25 else 'Other'

enter image description here

This is the image that I run first which doesn't show any error

a = np.vectorize(quality)
df['q2'] = a(df['total_bill'],df['tip'])
df['q1'] = np.vectorize(quality)(df['total_bill'],df['tip'])

enter image description here

1 Answer 1

1

You can vectorize solution different way - with numpy.where instead lambda and np.vectorize:

df['q1'] = np.where(df['tip']/df['total_bill']>0.25, 'Generous' ,'Other')

EDIT:

After some research for correct working need pass x, y to lambda and also change selecting by columns in lambda function like, because you pass 2 columns to function:

df = pd.DataFrame({
    'tip':[1,2,6],
    'total_bill':[1,5,1]  })

quality = lambda x, y:'Generous' if x/y>0.25 else 'Other'

a = np.vectorize(quality)
df['q2'] = a(df['total_bill'],df['tip'])
df['q1'] = np.vectorize(quality)(df['total_bill'],df['tip'])
print (df)

   tip  total_bill        q2        q1
0    1           1  Generous  Generous
1    2           5  Generous  Generous
2    6           1     Other     Other
Sign up to request clarification or add additional context in comments.

2 Comments

Getting answer is not the problem. I just want to know why it's not working now but worked before. This answer is quite great. Thanks for the answer
@Deepan - Unfortuantely not idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.