[英]Pandas Check if a Row Exists Anywhere in a Column and Return True or False
I'm trying to check is a row is present anywhere in another column and return True / False in a column called 'Check'.我正在尝试检查另一列中的任何位置是否存在一行,并在名为“检查”的列中返回 True/False。
Specifically I'm trying to lookup the values in a column called 'Keyword', and check if it exists anywhere in a column called description.具体来说,我试图在名为“关键字”的列中查找值,并检查它是否存在于名为 description 的列中的任何位置。
Each keyword to check is 2 words+ and I'm looking to find the word in the exact order.要检查的每个关键字都是 2 个字以上,我希望按照确切的顺序找到该字。
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.您可以对值
list
使用Series.isin
方法。 So you need a proper list
of Description
column values:因此,您需要一个正确的
Description
列值list
:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.