pandas - 熊猫包含不匹配的完整字符串
问题描述
我在使用这个.contains
功能时遇到了问题df
。为什么它与我的字符串不匹配?显然df
有字符串。它只匹配“Chief”。
import pandas as pd
link = 'https://www.sec.gov/Archives/edgar/data/1448056/000119312518215760/d619223ddef14a.htm'
ceo = 'Chief Executive Officer'
df_list = pd.read_html(link)
df = df_list[62]
df = df.fillna('')
for column in df:
if column == 4:
print ('try #1', df[column].str.contains(ceo, case=True, regex=True))
print ('try #2', df[column].str.contains(ceo, case=True, regex=False))
print ('try #3', df[column].str.contains(ceo, regex=False))
print ('try #4', df[column].str.contains(ceo, regex=True))
print ('try #5', df[column].str.contains(pat=ceo, regex=False))
print ('try #6', df[column].str.contains(pat=ceo, case=True, regex=True))
解决方案
问题是编码,如果你这样做,你可以看到它:
df[4].iloc[2]
因为它打印:
'Founder,\xa0Chief\xa0Executive\xa0Officer,\xa0and\xa0Director'
要修复它,请使用 unidecode:
import unidecode
for column in df.columns:
if column == 4:
print ('try #1', df[column].apply(lambda x:
unidecode.unidecode(x)).str.contains(ceo, case=True, regex=True))
print ('try #2', df[column].apply(lambda x:
unidecode.unidecode(x)).str.contains(ceo, case=True, regex=False))
print ('try #3', df[column].apply(lambda x:
unidecode.unidecode(x)).str.contains(ceo, regex=False))
print ('try #4', df[column].apply(lambda x:
unidecode.unidecode(x)).str.contains(ceo, regex=True))
print ('try #5', df[column].apply(lambda x:
unidecode.unidecode(x)).str.contains(pat=ceo, regex=False))
print ('try #6', df[column].apply(lambda x:
unidecode.unidecode(x)).str.contains(pat=ceo, case=True, regex=True))
推荐阅读
- python - 有没有更好的方法从熊猫数据框中获取一条数据
- python - Keras,Tensorflow - 计算指标时K.epsilon的含义是什么
- python - Python Rock, Paper, Scissors 将剪刀视为无效选择
- javascript - 通过 WebAuthn API 识别手指 ID
- node.js - URL 绑定问题 Vue/webpack
- node.js - 如何使用 NodeJS 以间隔发出 API 请求
- javascript - 使用 Javascript 中的选择框搜索和过滤结果
- django - django 视图部分中的代码混淆
- kubernetes - Flink Statefun HA Kubernetes 集群
- c++ - 如何强制编译器在寄存器中生成关键变量?