在pandas中,如何从基于另一个数据框的数据框中删除行?

在pandas中,如何从基于另一个数据框的数据框中删除行?,第1张

在pandas中,如何从基于另一个数据框的数据框中删除行?

您可以将

boolean indexing
和条件一起使用
isin
Series
通过
~
以下方式反转布尔值:

import pandas as pdUSERS = pd.Dataframe({'email':['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]']})print (USERS)     email0  [email protected]  [email protected]  [email protected]  [email protected]  [email protected] = pd.Dataframe({'email':['[email protected]','[email protected]']})print (EXCLUDE)     email0  [email protected]  [email protected] (USERS.email.isin(EXCLUDE.email))0     True1    False2    False3    False4     TrueName: email, dtype: boolprint (~USERS.email.isin(EXCLUDE.email))0    False1     True2     True3     True4    FalseName: email, dtype: boolprint (USERS[~USERS.email.isin(EXCLUDE.email)])     email1  [email protected]  [email protected]  [email protected]

另一个解决方案

merge

df = pd.merge(USERS, EXCLUDE, how='outer', indicator=True)print (df)     email     _merge0  [email protected]       both1  [email protected]  left_only2  [email protected]  left_only3  [email protected]  left_only4  [email protected]       bothprint (df.loc[df._merge == 'left_only', ['email']])     email1  [email protected]  [email protected]  [email protected]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5654560.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存