pandas.Series.str.count#
- Series.str.count(pat, flags=0)[源代码]#
统计序列/索引的每个字符串中模式的出现次数。
此函数用于计算特定正则表达式模式在
Series
。- 参数
- pat应力
有效的正则表达式。
- flagsInt,默认为0,表示无标志
的旗帜 re module. For a complete list, see here 。
- **kwargs
与其他字符串方法的兼容性。没有用过。
- 退货
- 系列或指数
与包含整数计数的调用对象相同的类型。
注意事项
传入时需要对某些字符进行转义 pat 。例如。
'$'
在正则表达式中具有特殊含义,在找到此原义字符时必须进行转义。示例
>>> 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') Int64Index([0, 0, 2, 1], dtype='int64')