【发布时间】:2020-05-28 13:52:43
【问题描述】:
给定一个充满电子邮件的数据框,我想过滤掉包含可能被阻止的域名或明显虚假电子邮件的行。下面的数据框代表我的数据示例。
>> print(df)
email number
1 fake@fake.com 2
2 real.email@gmail.com 1
3 no.email@email.com 5
4 real@yahoo.com 2
5 rich@money.com 1
我想按两个列表进行过滤。第一个列表是fake_lst = ['noemail', 'noaddress', 'fake', ... 'no.email']。
第二个列表只是将集合from disposable_email_domains import blocklist 转换为列表(或作为集合保留)。
当我使用df = df[~df['email'].str.contains('noemail')] 时,它可以正常工作并过滤掉该条目。然而,当我做df = df[~df['email'].str.contains(fake_lst)] 时,我得到TypeError: unhashable type: 'list'。
显而易见的答案是在许多其他 stackoverflow 问题中使用 df = df[~df['email'].isin(fake_lst)],例如 Filter Pandas Dataframe based on List of substrings 或 pandas filtering using isin function,但最终没有任何效果。
我想我可以为每个可能的列表条目使用str.contains('string'),但这太麻烦了。
因此,我需要根据两个列表中包含的子字符串过滤此数据帧,以便删除包含我想要删除的特定子字符串的任何电子邮件,以及包含它的后续行。
在上面的例子中,过滤后的数据框是:
>> print(df)
email number
2 real.email@gmail.com 1
4 real@yahoo.com 2
5 rich@money.com 1
【问题讨论】:
标签: python pandas dataframe filter substring