pandas ValueError:模式不包含捕获组 [英] pandas ValueError: pattern contains no capture groups

查看:608
本文介绍了 pandas ValueError:模式不包含捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用正则表达式时,我得到:

When using regular expression, I get:

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

如何解决问题?

谢谢.

推荐答案

根据

According to the docs, you need to specify a capture group (i.e., parentheses) for str.extract to, well, extract.

Series.str.extract(pat, flags=0, expand=True)
对于每个主题 系列中的字符串,从常规的第一个匹配项中提取组 表达方式.

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.

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

Each capture group constitutes its own column in the output.

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

或者,如果您需要系列作品,

Or, if you need a Series,

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

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

这篇关于 pandas ValueError:模式不包含捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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