Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if any string in a list of strings is in a pandas row and return bool result

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading