Reputation: 87
I have a code like:
def sample_func(new_df):
if ( new_df['name'] == 'Tom'):
return "Yes"
elif( new_df['name'].isin(['Harry', 'Jerry', 'Savi', 'Aavi'])):
return "Common Name"
else:
return None
I am getting error as:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
How to fix such errors?
Upvotes: 2
Views: 289
Reputation: 837
I have modified your code with any
to get the result:-
def sample_func(new_df):
if any( new_df['name'] == 'Tom'):
return "Yes"
elif any( new_df['name'].isin(['Harry', 'Jerry', 'Savi', 'Aavi'])):
return "Common Name"
else:
return None
Output
Yes
I hope it may help you.
Upvotes: 0
Reputation: 862641
Use numpy.select
:
def sample_func(new_df):
m1 = new_df['name'] == 'Tom'
m2 = new_df['name'].isin(['Harry', 'Jerry', 'Savi', 'Aavi'])
new_df['new'] = np.select([m1, m2], ['Yes','Common Name'], default=None)
return new_df
More information about your error is here.
Upvotes: 3