【发布时间】:2020-07-12 17:32:57
【问题描述】:
我有 2 个数据框:
df1 是邮箱和电子邮件 ID 的列表
df2 显示已批准域的列表
我从 Excel 表中读取了两个数据框
xls = pd.ExcelFile(input_file_shared_mailbox)
df = pd.read_excel(xls, sheet_name = sheet_name_shared_mailbox)
我只想在 df1[Email_Id] 包含 df2[approved_domain] 的 df1 中保留记录
print(df1)
Mailbox Email_Id
0 mailbox1 abc@gmail.com
1 mailbox2 def@yahoo.com
2 mailbox3 ghi@msn.com
print(df2)
approved_domain
0 msn.com
1 gmail.com
我想要基本上显示的 df3
print (df3)
Mailbox Email_Id
0 mailbox1 abc@gmail.com
1 mailbox3 ghi@msn.com
这是我现在拥有的代码,我认为它很接近,但我无法弄清楚语法中的确切问题
df3 = df1[df1['Email_Id'].apply(lambda x: [item for item in x if item in df2['Approved_Domains'].tolist()])]
但是得到这个错误
TypeError: unhashable type: 'list'
我花了很多时间研究论坛的解决方案,但找不到我要找的东西。感谢所有帮助。
【问题讨论】:
-
粘贴你的代码,尤其是df1和df2的定义
-
更新了帖子,我从 excel 选项卡中读取并使用我在主帖子中放入的代码将其加载到 df 中
标签: python string dataframe contains partial