加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – pandas ValueError:pattern不包含任何捕获组

发布时间:2020-12-20 11:09:21 所属栏目:Python 来源:网络整理
导读:使用正则表达式时,我得到: import restring = r'http://www.example.com/abc.html'result = re.search('^.*com',string).group() 在熊猫中,我写道: df = pd.DataFrame(columns = ['index','url'])df.loc[len(df),:] = [1,'http://www.example.com/abc.html
使用正则表达式时,我得到:

import re
string = r'http://www.example.com/abc.html'
result = re.search('^.*com',string).group()

在熊猫中,我写道:

df = pd.DataFrame(columns = ['index','url'])
df.loc[len(df),:] = [1,'http://www.example.com/abc.html']
df.loc[len(df),:] = [2,'http://www.hello.com/def.html']
df.str.extract('^.*com')

ValueError: pattern contains no capture groups

如何解决问题?

谢谢.

解决方法

根据 docs,您需要为str.extract指定捕获组(即括号),以及提取.

Series.str.extract(pat,flags=0,expand=True)
For each subject
string in the Series,extract groups from the first match of regular
expression pat.

每个捕获组在输出中构成其自己的列.

df.url.str.extract(r'(.*.com)')

                        0
0  http://www.example.com
1    http://www.hello.com
# If you need named capture groups,df.url.str.extract(r'(?P<URL>.*.com)')

                      URL
0  http://www.example.com
1    http://www.hello.com

或者,如果你需要一个系列,

df.url.str.extract(r'(.*.com)',expand=False)

0    http://www.example.com
1      http://www.hello.com
Name: url,dtype: object

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读