我遇到了以下问题:有2个数据帧,比如:
importdf:
System Email
1 Basic testimail@yahoo.com
2 Basic anotheremail@yahoo.com
3 Backend newemail@yahoo.com
userdf:
System Email
1 Basic testimail@yahoo.com
2 Backend anotheremail@yahoo.com
3 Basic newemail@yahoo.com
我需要做的是删除importdf中存在于userdf中的每一行。因为我在原始数据帧中获得了一些具有不同数据的额外列,所以我不能只告诉pandas删除重复的行。此时,我将使用以下代码来处理此问题:
importdf_system = importdf['System'].tolist()
importdf_emails = importdf['Email'].tolist()
userdf_system = userdf['System'].tolist()
userdf_emails = userdf['Email'].tolist()
importdf.reset_index(drop=True)
userdf.reset_index(drop=True)
counter = len(importdf)
for i in range(len(importdf)):
counter = counter - 1
print(counter)
for j in range(len(userdf)):
if "@" in str(importdf_emails[i]) and "@" in str(userdf_emails[j]) and str(importdf_emails[i]).lower() == str(userdf_emails[j]).lower():
importdf = importdf.drop([i])
有时,该代码运行良好,但需要数小时才能运行,因为数据帧非常庞大。而且,有时,我会遇到类似KeyError: '[1782] not found in axis'
的错误
我一直在寻找更好的方法,但没有找到有用的解决办法。找到了一种使用1列查找现有数据的方法,但问题是,只有在系统和电子邮件相同的情况下,我才需要删除行。例如,如果我收到同一封电子邮件,但同一行有不同的系统,它必须保持不变
与新行保持一致
#{}te
IIUC,您可以执行左合并并指定仅在左数据帧中的值
细节
相关问题 更多 >
编程相关推荐