Reputation: 211
So this works:
idocs[idocs['Message'].str.contains('No sold-to found')]
But this doesn't:
def msg_logic(msg):
if msg.str.contains('No sold-to found'):
return 'No Sold-to'
else:
return 'Other'
idocs['Category'] = idocs.apply(lambda x: msg_logic(x['Message']),axis=1)
print(idocs['Category'].unique())
idocs.head()
Upvotes: 1
Views: 37
Reputation: 328
def msg_logic(msg):
if 'No sold-to found' in msg:
return 'No Sold-to'
else:
return 'Other'
idocs['Category'] = idocs.apply(lambda x: msg_logic(x['Message']),axis=1)
print(idocs['Category'].unique())
idocs.head()
Your code does not run because msg
in the function is a string no a dataframe.
or
idocs['Category'] = idocs['Message'].apply(lambda x: "No Sold-to" if "No sold-to found" in x else "Other")
Upvotes: 1
Reputation: 59274
I recommend np.where
idocs['Category'] = (np.where(idocs['Message'].str.contains('No sold-to found'),
'No Sold-to',
'Other'))
For multiple conditions, use np.select
Upvotes: 2
Reputation: 7353
Try this.
idocs['Category'] = idocs['Message'].str.contains('No sold-to found')
idocs['Category'] = idocs['Category'].apply(lambda x: 'No Sold-to' if x else 'Other')
Upvotes: 0