Skip to content
Advertisement

pandas dataframe str.contains() AND operation

I have a df (Pandas Dataframe) with three rows:

JavaScript

The function df.col_name.str.contains("apple|banana") will catch all of the rows:

JavaScript

How do I apply AND operator to the str.contains() method, so that it only grabs strings that contain BOTH “apple” & “banana”?

JavaScript

I’d like to grab strings that contains 10-20 different words (grape, watermelon, berry, orange, …, etc.)

Advertisement

Answer

You can do that as follows:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement