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)
>Solution :
Use Series.str.contains with join list for regex OR:
L = ['foo','box']
clean_df['to_process'] = clean_df['sentence'].str.contains('|'.join(L))