Supposing that I have a pandas.DataFrame
and a list of strings:
fruit = ['apple', 'banana', 'cherry' ] # etc
Is there a neat way to select rows in which the value of that column contains (has as a substring) one of the strings from the list?
I'm aware of isin
, which checks the whole value of the column against a list of possibilities:
df[df['column'].isin(fruit)]
and contains
, which matches it against a single string:
df[df['column'].str.contains('apple')]
Neither of those seems to be quite what I want. In other languages I could do something with a loop, but I'm quite new to the pandas style of working with data so unsure how to proceed. The list of strings is also quite large (a couple of thousand items) so compiling it into a regex to match against doesn't seem like it would be a good idea.