我有一个带有ID和一些电子邮件地址的数据框
personid sup1_email sup2_email sup3_email sup4_email
1 evan.o@abc.com jon.k@abc.com kelm.q@abc.com john.d@abc.com
5 evan.o@abc.com polly.u@abc.com jim.e@ABC.COM nan
11 jim.y@abc.com manfred.a@abc.com greg.s@Abc.com adele.a@abc.com
52 jim.y@abc.com manfred.a@abc.com greg.s@Abc.com adele.a@abc.com
65 evan.o@abc.com lenny.t@yahoo.com john.s@abc.com sally.j@ABC.com
89 dom.q@ABC.com laurie.g@Abc.com topher.u@abc.com ross.k@qqpower.com
我想找到与接受的电子邮件值列表不匹配的行(即不是“@abc.com”、“@abc.com”、“@abc.com”)。我想要的是这个
personid sup1_email sup2_email sup3_email sup4_email
65 evan.o@abc.com lenny.t@yahoo.com john.s@abc.com sally.j@ABC.com
89 dom.q@ABC.com laurie.g@Abc.com topher.u@abc.com ross.k@qqpower.com
我已经编写了以下代码,它可以正常工作,但我必须手动检查每个sup_电子邮件列并重复该过程,这是低效的
#list down all the variations of accepted email domains
email_adds = ['@abc.com','@ABC.COM','@Abc.com']
#combine the variations of email addresses in the list
accepted_emails = '|'.join(email_adds)
not_accepted = df.loc[~df['sup1_email'].str.contains(accepted_emails, na=False)]
我想知道是否有更有效的方法使用for循环来实现这一点。到目前为止,我所做的是显示一个包含未接受电子邮件的列,但它没有显示包含未接受电子邮件的行。谢谢你,我能得到任何形式的帮助
sup_emails = df[['sup1_email','sup2_email', 'sup3_email', 'sup4_email']]
#for each sup column, check if the accepted email addresses are not in it
for col in sup_emails:
if any(x not in col for x in accepted_emails):
print(col)
一个想法:
使用生成器和
any
的解决方案:让我们尝试检查所有列中
@
之后的字符是否为ABC
或abc
或Abc
。当然,我们可以临时过滤掉PersonID
。检查后,使用~
反转结果并屏蔽它们你可以做:
输出
相关问题 更多 >
编程相关推荐