我在数据框中有一列,例如 df:
A
0 Good to 1. Good communication EI : [email protected]
1 SAP ECC Project System EI: ram.va[email protected]
2 EI : ravikumar.swarna Role:SSE Minimum Skill
我有一个字符串列表
ls=['[email protected]','[email protected]']
现在如果我想过滤掉
for i in range(len(ls)):
df1=df[df['A'].str.contains(ls[i])
if len(df1.columns!=0):
print ls[i]
我得到输出
[email protected]
[email protected]
但我只需要[email protected]
如何实现?
正如你所看到的,我已经尝试过字符串包含但我需要一些精确匹配的东西
你可以简单地使用==
string_a == string_b
如果两个字符串相等,它应该返回 True。但这并不能解决你的问题。
Edit 2:您应该使用 len(df1.index) 而不是 len(df1.columns)。事实上, len(df1.columns) 会给你列数,而不是行数。
Edit 3:读完你的第二篇文章后,我明白了你的问题。您提出的解决方案可能会导致一些错误。
例如,如果您有:
ls=['[email protected]','[email protected]', '[email protected]']
第一个和第三个元素将匹配 str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i])
这是一种不受欢迎的行为。
您可以在字符串末尾添加检查: str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s| $)')
像这样:
for i in range(len(ls)):
df1 = df[df['A'].str.contains(r'(?:\s|^|Ei:|EI:|EI-)'+ls[i]+r'(?:\s|$)')]
if len(df1.index != 0):
print (ls[i])
(如果使用 python 2.7,请删除“print”中的括号)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)