I'm trying to check is a row is present anywhere in another column and return True / False in a column called 'Check'.
Specifically I'm trying to lookup the values in a column called 'Keyword', and check if it exists anywhere in a column called description.
Each keyword to check is 2 words+ and I'm looking to find the word in the exact order.
Keyword Description Check
spam spam spam spam foo bar True
spam foo True
spam bar False
spam spam foo True
spam bar False
My code:
df['Check'] = df.apply(lambda row: row['Keyword'] in row['Description'], axis=1)
This checks if the keyword is in a matching row, but I need to check if it existing anywhere in the entire column instead. Thanks!
You can use Series.isin
method against a list
of values. So you need a proper list
of Description
column values:
In [915]: vals = [x.split() for x in df.Description.values][0]
In [917]: df['Check'] = df.Keyword.isin(vals)
In [918]: df
Out[918]:
Keyword Description Check
0 spam eggs spam foo bar True
1 eggs True
2 house False
3 foo True
4 bar True
5 turtle False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.