Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (17.6k points)

Is there any function that would be the equivalent of a combination of df.isin() and df[col].str.contains()?

For example, say I have the series s = pd.Series(['cat','hat','dog','fog','pet']), and I want to find all places where s contains any of ['og', 'at'], I would want to get everything but 'pet'.

I have a solution, but it's rather inelegant:

searchfor = ['og', 'at']

found = [s.str.contains(x) for x in searchfor]

result = pd.DataFrame[found]

result.any()

Is there a better way to do this?

1 Answer

0 votes
by (41.4k points)

Use str.contains with a regex pattern using OR (|):

s[s.str.contains('og|at')]

Output:

0 cat

1 hat

2 dog

3 fog 

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

Browse Categories

...