Categories
Python Answers

How to test if a string contains one of the substrings in a list, in Python Pandas?

Spread the love

Sometimes, we want to test if a string contains one of the substrings in a list, in Python Pandas.

In this article, we’ll look at how to test if a string contains one of the substrings in a list, in Python Pandas.

How to test if a string contains one of the substrings in a list, in Python Pandas?

To test if a string contains one of the substrings in a list, in Python Pandas, we can call str.contains.

For instance, we erite

searchfor = ['og', 'at']
r = s[s.str.contains('|'.join(searchfor))]

to call s.str.contains with '|'.join(searchfor) to search for strings listed in searchfor in our series s.

Conclusion

To test if a string contains one of the substrings in a list, in Python Pandas, we can call str.contains.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *