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
quality = lambda x:'Generous' if x['tip']/x['total_bill']>0.25 else 'Other'
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'])
Advertisement
Answer
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