pyspark.pandas.Series.str.count

str.count(pat: str, flags: int = 0) → ps.Series

Count occurrences of pattern in each string of the Series.

This function is used to count the number of times a particular regex pattern is repeated in each of the string elements of the Series.

Parameters
patstr

Valid regular expression.

flagsint, default 0 (no flags)

Flags for the re module.

Returns
Series of int

A Series containing the integer counts of pattern matches.

Examples

>>> s = ps.Series(['A', 'B', 'Aaba', 'Baca', np.NaN, 'CABA', 'cat'])
>>> s.str.count('a')
0    0.0
1    0.0
2    2.0
3    2.0
4    NaN
5    0.0
6    1.0
dtype: float64

Escape ‘$’ to find the literal dollar sign.

>>> s = ps.Series(['$', 'B', 'Aab$', '$$ca', 'C$B$', 'cat'])
>>> s.str.count('\$')
0    1
1    0
2    1
3    2
4    2
5    0
dtype: int64