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.