pandas.Series.str.count#
- Series.str.count(pat, flags=0)[源代码]#
计算 Series/Index 中每个字符串中模式的出现次数。
此函数用于计算正则表达式模式在
Series的每个字符串元素中重复出现的次数。- Parameters:
- patstr
有效的正则表达式。
- flagsint,默认为 0,表示无标志。
re 模块的标志。有关完整列表,请`see here <https://docs.python.org/3/howto/regex.html#compilation-flags>`_ 。
- **kwargs
为了与其他字符串方法兼容。未使用。
- Returns:
- Series 或 Index
与包含整数计数的调用对象相同的类型。
Notes
在传递 pat 时,某些字符需要转义。例如,
'$'在正则表达式中具有特殊含义,在查找此字面字符时必须转义。Examples
>>> s = pd.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
转义
'$'以查找字面上的美元符号。>>> s = pd.Series(['$', 'B', 'Aab$', '$$ca', 'C$B$', 'cat']) >>> s.str.count('\\$') 0 1 1 0 2 1 3 2 4 2 5 0 dtype: int64
这也适用于 Index。
>>> pd.Index(['A', 'A', 'Aaba', 'cat']).str.count('a') Index([0, 0, 2, 1], dtype='int64')