pandas String manipulation Checking for contents of a string


str.contains() method can be used to check if a pattern occurs in each string of a Series. str.startswith() and str.endswith() methods can also be used as more specialized versions.

In [1]: animals = pd.Series(['cat', 'dog', 'bear', 'cow', 'bird', 'owl', 'rabbit', 'snake'])

Check if strings contain the letter 'a':

In [2]: animals.str.contains('a')
0      True
1     False
2      True
3     False
4     False
5     False
6      True
7      True
8      True
dtype: bool

This can be used as a boolean index to return only the animals containing the letter 'a':

In [3]: animals[animals.str.contains('a')]
0       cat
2      bear
6    rabbit
7     snake
dtype: object

str.startswith and str.endswith methods work similarly, but they also accept tuples as inputs.

In [4]: animals[animals.str.startswith(('b', 'c'))]
# Returns animals starting with 'b' or 'c'
0     cat
2    bear
3     cow
4    bird
dtype: object