python笔记:str.contains小坑之 UserWarning To actually get the groups, use str.extract.
作者:互联网
df = pd.DataFrame({'name': ['A(上海)AAA', 'BB(上海)BB', 'CCC', 'DDD']}) print(df) # 结果如下 name 0 A(上海)AAA 1 BB(上海)BB 2 CCC 3 DDD
问题:
代码返回如下结果:
UserWarning: This pattern has match groups. To actually get the groups, use >str.extract. return func(self, *args, **kwargs) name 2 CCC a = ['A(上海)AAA', 'BB(上海)BB', 'CCC'] b = df[df.name.str.contains('|'.join(a))] print(b)
分析:
原因1:str.contains()不直接支持对象里边的
括号
,因为括号
是正则表达式之一。解决办法:
a = ['A\(上海\)AAA', 'BB\(上海\)BB', 'CCC'] # 使用转义符 “\” # 或者 a = ['A.上海.AAA', 'BB.上海.BB', 'CCC'] b = df[df.name.str.contains('|'.join(a))] print(b) # 结果如下: name # 没有UserWarning了 0 A(上海)AAA 1 BB(上海)BB 2 CCC
标签:use,AAA,str,BB,df,groups,CCC,name 来源: https://www.cnblogs.com/lnd-blog/p/14987346.html