pandas.Series.str.match和pandas.Series.str.之间的区别 [英] difference between pandas.Series.str.match and pandas.Series.str.contains

查看:579
本文介绍了pandas.Series.str.match和pandas.Series.str.之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pandas.Series.str.containspandas.Series.str.match有什么区别?为什么会出现以下情况?

What's the difference between pandas.Series.str.contains and pandas.Series.str.match? Why is the case below?

s1 = pd.Series(['house and parrot'])
s1.str.contains(r"\bparrot\b", case=False)

我得到了True,但是当我这样做时

I got True, but when i do

s1.str.match(r"\bparrot\b", case=False)

我得到了False.为什么会这样?

I got False. Why is the case?

推荐答案

测试模式或正则表达式是否包含在Series或 索引.

Test if pattern or regex is contained within a string of a Series or Index.

str的文档. match()状态:

确定每个字符串是否与正则表达式匹配.

Determine if each string matches a regular expression.

这两种方法的区别在于str.contains()使用的是re.search,而str.match()使用的是re.match.

The difference in these two methods is that str.contains() uses: re.search, while str.match() uses re.match.

根据 re.match()的文档进行

如果字符串开头的零个或多个字符与 正则表达式模式,返回相应的match对象. 如果字符串与模式不匹配,则返回None;否则返回false.请注意, 与零长度匹配不同.

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.

因此parrot与字符串的第一个字符不匹配,因此您的表达式返回False.房屋确实与第一个字符匹配,因此找到房屋并返回true.

So parrot does not match the first character of the string so your expression returns False. House does match the first character so it finds house and returns true.

这篇关于pandas.Series.str.match和pandas.Series.str.之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆