@@ -212,15 +212,65 @@ def str_count(arr, pat, flags=0):
212
212
"""
213
213
Count occurrences of pattern in each string of the Series/Index.
214
214
215
+ This function is used to count the number of times a particular regex
216
+ pattern is repeated in each of the string elements of the
217
+ :class:`~pandas.Series`.
218
+
215
219
Parameters
216
220
----------
217
- pat : string, valid regular expression
218
- flags : int, default 0 (no flags)
219
- re module flags, e.g. re.IGNORECASE
221
+ pat : str
222
+ Valid regular expression.
223
+ flags : int, default 0, meaning no flags
224
+ Flags for the `re` module. For a complete list, `see here
225
+ <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
226
+ **kwargs
227
+ For compatability with other string methods. Not used.
220
228
221
229
Returns
222
230
-------
223
- counts : Series/Index of integer values
231
+ counts : Series or Index
232
+ Same type as the calling object containing the integer counts.
233
+
234
+ Notes
235
+ -----
236
+ Some characters need to be escaped when passing in `pat`.
237
+ eg. ``'$'`` has a special meaning in regex and must be escaped when
238
+ finding this literal character.
239
+
240
+ See Also
241
+ --------
242
+ re : Standard library module for regular expressions.
243
+ str.count : Standard library version, without regular expression support.
244
+
245
+ Examples
246
+ --------
247
+ >>> s = pd.Series(['A', 'B', 'Aaba', 'Baca', np.nan, 'CABA', 'cat'])
248
+ >>> s.str.count('a')
249
+ 0 0.0
250
+ 1 0.0
251
+ 2 2.0
252
+ 3 2.0
253
+ 4 NaN
254
+ 5 0.0
255
+ 6 1.0
256
+ dtype: float64
257
+
258
+ Escape ``'$'`` to find the literal dollar sign.
259
+
260
+ >>> s = pd.Series(['$', 'B', 'Aab$', '$$ca', 'C$B$', 'cat'])
261
+ >>> s.str.count('\$')
262
+ 0 1
263
+ 1 0
264
+ 2 1
265
+ 3 2
266
+ 4 2
267
+ 5 0
268
+ dtype: int64
269
+
270
+ This is also available on Index
271
+
272
+ >>> pd.Index(['A', 'A', 'Aaba', 'cat']).str.count('a')
273
+ Int64Index([0, 0, 2, 1], dtype='int64')
224
274
"""
225
275
regex = re .compile (pat , flags = flags )
226
276
f = lambda x : len (regex .findall (x ))
0 commit comments