maxframe.dataframe.Series.str.count#

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

计算 Series/Index 中每个字符串中模式出现的次数。

此函数用于计算特定正则表达式模式在 Series 的每个字符串元素中重复的次数。

参数:
返回:

与调用对象类型相同,包含整数计数。

返回类型:

Series or Index

参见

re

用于正则表达式的标准库模块。

str.count

标准库版本,不支持正则表达式。

备注

传入 pat 时,某些字符需要转义。例如,'$' 在正则表达式中有特殊含义,查找该字面字符时必须转义。

示例

>>> import maxframe.tensor as mt
>>> import maxframe.dataframe as md
>>> s = md.Series(["A", "B", "Aaba", "Baca", mt.nan, "CABA", "cat"])
>>> s.str.count("a").execute()
0    0.0
1    0.0
2    2.0
3    2.0
4    NaN
5    0.0
6    1.0
dtype: float64

转义 '$' 以查找字面美元符号。

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

这在 Index 上也可用

>>> md.Index(["A", "A", "Aaba", "cat"]).str.count("a").execute()
Index([0, 0, 2, 1], dtype='int64')