- API reference
- Series
- pandas.Serie...
pandas.Series.str.count#
- Series.str.count(pat,flags=0)[source]#
Count occurrences of pattern in each string of the Series/Index.
This function is used to count the number of times a particular regexpattern is repeated in each of the string elements of the
Series.- Parameters:
- patstr
Valid regular expression.
- flagsint, default 0, meaning no flags
Flags for there module. For a complete list,see here.
- **kwargs
For compatibility with other string methods. Not used.
- Returns:
- Series or Index
Same type as the calling object containing the integer counts.
See also
Notes
Some characters need to be escaped when passing inpat.eg.
'$'has a special meaning in regex and must be escaped whenfinding this literal character.Examples
>>>s=pd.Series(['A','B','Aaba','Baca',np.nan,'CABA','cat'])>>>s.str.count('a')0 0.01 0.02 2.03 2.04 NaN5 0.06 1.0dtype: float64
Escape
'$'to find the literal dollar sign.>>>s=pd.Series(['$','B','Aab$','$$ca','C$B$','cat'])>>>s.str.count('\\$')0 11 02 13 24 25 0dtype: int64
This is also available on Index
>>>pd.Index(['A','A','Aaba','cat']).str.count('a')Index([0, 0, 2, 1], dtype='int64')
On this page