Check if any string in a list of strings is in a pandas row and return bool result
Question:
I want to return bool column based on a condition:
- column with sentences
- list = [‘foo’, ‘box’]
- if any from list in row -> return True, else return False
My code does not work and I can’t find the mistake:
clean_df['to_process'] = clean_df['sentence'].apply(
lambda x: True if any(st in x for st in ['foo','box']) else False)
Answers:
Use Series.str.contains
with join list for regex OR
:
L = ['foo','box']
clean_df['to_process'] = clean_df['sentence'].str.contains('|'.join(L))
I want to return bool column based on a condition:
- column with sentences
- list = [‘foo’, ‘box’]
- if any from list in row -> return True, else return False
My code does not work and I can’t find the mistake:
clean_df['to_process'] = clean_df['sentence'].apply(
lambda x: True if any(st in x for st in ['foo','box']) else False)
Use Series.str.contains
with join list for regex OR
:
L = ['foo','box']
clean_df['to_process'] = clean_df['sentence'].str.contains('|'.join(L))