[英]Check if multiple strings are present in pandas dataframe
我试图确定是否在dataframe
找到两个变量
table:
Code index
600.SI 4th Q 2015
500.SI Full Year
ggr.SI 1st Q 2016
# If variable_code and variable_date is not found in table, print not found
if table['Code'].str.contains(variable_code).any() & table['index'].str.contains(variable_date).any():
print('found')
else:
print('not found')
但是,它总是让我found
。
我认为我的if
语句结构不正确,无法对两个项目进行bool
比较。
如何解决呢?
更新资料
通常,可以在table
找到variable_code
。 如果在table
找到了variable_code
,请检查是否也存在variable_date
。
我要达到的目的是,如果这两个条件都不存在,则not found
打印。
更换&
与and
。 &
是按位AND运算符。
使用str.contains
,默认情况下regex=True
,因此,如果您不小心,将不会匹配正确的东西。 如果要检查相等性而不是包含性,请使用==
运算符,如下所示:
if not table[(table['Code'] == variable_code)
& (table['index'] == variable_date)].empty: # https://stackoverflow.com/a/45780191/4909087
print('Found')
else:
print('Not found')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.