Reputation: 119
My purpose is to get below keywords in a column name 'Body' and 'Original Sender ID' that contains below keywords and assign a value of 1 to a column name 'New Value'.
The 'searching' must be an exact keywords, for an example "Line" cannot be detected as "online".
I have set the the "New Value" column value 0 as default.
keywords = ('WhatsApp', 'Google' , 'Michat' , 'Line')
df3[df3['Body'].str.contains(keywords) | df3['Original Sender ID'].str.conatins(keywords)]
df3.loc['New Value'] = '1'
Appreciate your help! :)
Upvotes: 0
Views: 527
Reputation: 584
Fastest way to do this would be through np.where() and construct the list to search from with a join operator. The str.contains() will make sure only an exact match is flagged.
import pandas as pd
import numpy as np
data = {'Body': ['Whatsapp', 'bar','What', "contains WhatsApp"],
'Original Sender ID': ['foo', 'also contains WhatsApp','baz', "App"]}
df = pd.DataFrame (data)
keywords = ['WhatsApp', 'Google' , 'Michat' , 'Line']
df["NewValue"] = np.where((df['Body'].str.contains('|'.join(keywords))) | (df['Original Sender ID'].str.contains('|'.join(keywords))),1,0)
Upvotes: 1