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 use the str.contains method with a regex pattern to find all the matches.

For instance, we write:

import pandas as pd

s = pd.Series(['cat', 'hat', 'dog', 'fog', 'pet'])
df = pd.DataFrame([('cat', 1000.0), ('hat', 2000000.0), ('dog', 1000.0),
                   ('fog', 330000.0), ('pet', 330000.0)],
                  columns=['col1', 'col2'])
r = df[s.str.contains('cat|pet')]
print(r)

We create a series with the pd.Series constructor.

Then we create a DataFrame with the pd.DataFrame constructor.

Next, we call s.str.contains with the words we’re looking for separated by a |.

And then we assign the matches to r.

Therefore, r is:

  col1      col2
0  cat    1000.0
4  pet  330000.0

Conclusion

To test if a string contains one of the substrings in a list in Python Pandas, we can use the str.contains method with a regex pattern to find all the matches.

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 *