python - 如何根据字符串列表在 df col 中查找字符串匹配?
问题描述
我有一份 1000 家公司的名单,以及当年所有以前交易的 df。对于每场比赛,我想在新列 (df$Covered) 中创建一个新行值 (True)。
我不确定为什么我不断收到以下错误。我尝试研究这些问题,但到目前为止还没有运气。
Pandas 从 df 中提取行,其中 df['col'] 值与 df2['col'] 值匹配
代码示例:当我设置 regex=False
Customer_List = ['3M','Cargill,'Chili's,---]
df['Covered'] = df[df['End Customer Name'].str.contains('|'.join(Customer_List),case=False, na=False, regex=False)]
ValueError:错误的项目数通过 32,位置意味着 1
代码示例:当我设置 regex=True
错误:位置 177825 处的错误字符范围 HD
~/opt/anaconda3/lib/python3.7/sre_parse.py in parse(str, flags, pattern)
928
929 try:
--> 930 p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
931 except Verbose:
932 **# the VERBOSE flag was switched on inside the pattern. to be**
~/opt/anaconda3/lib/python3.7/sre_parse.py in _parse_sub(source, state, verbose, nested)
424 while True:
425 itemsappend(_parse(source, state, verbose, nested + 1,
--> 426 **not nested and not items**))
427 if not sourcematch("|"):
428 break
解决方案
谢谢大家,这与我的 Customer_List 有特殊字符有关,所以我需要使用 map(re.escape
此链接帮助我了解 Python 正则表达式错误字符范围。
推荐阅读
- google-apps-script - 用输入参数 URL 替换链接中的文本
- oracle - 插入员工的 PLSQL 过程:ORA-00984
- android - 在 ListView 中使用带有简单适配器的过滤器
- python - 将一个列表插入到另一个列表中,每个元素都有索引
- asp.net - 如何在asp轮播循环中添加活动类
- ajax - Parsley 自定义验证器 (Ajax) 不会触发
- label - 如何在 FedEx 商业发票中生成多个项目描述
- docker - Docker 健康检查文件错误地找不到
- nginx - 为什么当我使用 nginx 时,nginx 的上游名称替换了我的地址栏?
- html - 一行两个div,但是一个div是固定大小的,怎么办?