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指定捕获组(即括号),以及提取.
每个捕获组在输出中构成其自己的列. 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 我怎么能弄清楚为什么django-allauth没有发送确认电子邮件?
- python – Seaborn / Matplotlib:如何在factorplot y轴上抑
- 精选26个Python实用技巧,想秀技能先Get这份技术列表!
- 如何使用另一个列表中的多个元素替换列表中的一个元素?
- python 实现各种排序算法!
- python – str和unicode之间的区别:为什么Redis在传递unic
- 如何从python中的另一个线程中止socket.recvfrom()?
- python – 如何获得信号的高低信封?
- Django rest framework----认证
- Django应用程序在ASP.NET MVC中等效?