How to match particular word in the list to pandas column?
Solution 1:
If we only want to see if there is a match, we can use str.contains
for the check and np.where
to assign values:
df['output'] = np.where(df['des'].str.contains('|'.join(a)), 'match', 'not match')
Output:
sr.no des output
0 1 2 bed rooms match
1 2 natural language processing not match
2 3 2x2 sheets grabs match
3 4 2 meter long pillow match
4 5 2x30mm long not match
Solution 2:
Dont use split
solutions, because in list are joined values by space
here 'admission kits'
, for avoid it use Series.str.extract
:
pat = r"\b({})\b".format("|".join(x for x in a))
df['output'] = df['des'].str.extract(pat).fillna('not match')
print (df)
sr.no des output
0 1 2 bed rooms bed
1 2 natural language processing not match
2 3 2x2 sheets grabs sheets
3 4 2 meter long pillow pillow
4 5 2x30mm long not match