我可以理解为什么 str.startswith() 不处理 Regex 吗:
col1
0 country
1 Country
i.e : df.col1.str.startswith('(C|c)ountry')
它返回所有值 False :
col1
0 False
1 False
Series.str.startswith
不接受正则表达式,因为它的行为类似于str.startswith
在普通 Python 中,它不接受正则表达式。另一种方法是使用正则表达式匹配(如所解释的在文档中 http://pandas.pydata.org/pandas-docs/stable/text.html#testing-for-strings-that-match-or-contain-a-pattern):
df.col1.str.contains('^[Cc]ountry')
字符类[Cc]
可能是更好的匹配方式C
or c
than (C|c)
,当然除非您需要捕获使用了哪个字母。在这种情况下你可以这样做([Cc])
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)