python - Check if multiple strings are present in pandas dataframe -
i trying determine if 2 variables found in dataframe
table: code index 600.si 4th q 2015 500.si full year ggr.si 1st q 2016 # if variable_code , variable_date 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') however, return me found.
i think if statement structured incorrectly bool comparison 2 items.
how can solved?
update
normally, variable_code found in table. if variable_code found in table, check if variable_date present too.
what trying achieve is, if both of these conditions not present, print not found.
with str.contains, regex=true default, if you're not careful, won't matching right things. if want check equality rather containment, use == operator, this:
if not table[(table['code'] == variable_code) & (table['index'] == variable_date)].empty: # https://stackoverflow.com/a/45780191/4909087 print('found') else: print('not found')
Comments
Post a Comment