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)
Asked By: Yana

||

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))
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.