简体   繁体   English

Pandas 检查列中是否存在某行并返回 True 或 False

[英]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.

相关问题 Pandas:如果列表中的值存在于行中的任何位置,则标记列 - Pandas: Flag column if value in list exists anywhere in row Pandas:如何创建 True/False 列来检查前几周具有某些值的现有行? - Pandas: How to create True/False column that check existing row with certain values in the previous weeks? Pandas DataFrame 检查日期数组中是否有日期并返回 True/False - Pandas DataFrame check if date in array of dates and return True/False Pandas 布尔检查意外返回 True 而不是 False - Pandas boolean check unexpectedly return True instead of False 检查pandas中是否存在行 - Check if a row exists in pandas 熊猫列和行存在 - Pandas Column and Row exists 检查 df1 中的字符串是否存在于 df2 中的任何位置并返回 df1 中匹配的列名 - Check if string in df1 exists anywhere in df2 and return the matching column name(s) in df1 如何检查pandas数据帧中是否存在具有特定列值的行 - How to check if there exists a row with a certain column value in pandas dataframe 要检查 Pandas Dataframe 列是否为 TRUE/FALSE,如果为 TRUE,请检查另一列是否满足条件并生成具有值 PASS/FAIL 的新列 - To check Pandas Dataframe column for TRUE/FALSE, if TRUE check another column for condition to satisfy and generate new column with values PASS/FAIL 在 ElasticSearch 中如何检查一个字段是否存在然后应该只返回布尔值( True 或 False ) - In ElasticSearch how to check if a field exists then should return Boolean value( True or False ) only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM