如何加入布尔值的两个数据范围并返回逆



我正在使用pandas itertuples方法在两个dataframes(df-a& df-b)之间执行行匹配。结果是DF-B的副本,只有传递的结果。

df-b之前:

   B   Y
0  2  10
1  4  15
2  6  15

df-b之后(AS DF-B2):

   B   Y     e
0  2  10  True
1  6  15  True

如何比较DF-B和DF-B2并仅返回缺失(暗示的错误)行?

   B   Y
1  4  15

这来自出色的熊猫备忘单,我认为它会做您想要的:

pd.merge(df-B, df-B2, how='outer',
    indicator=True)
    .query('_merge == "left_only"')
    .drop(['_merge'],axis=1)

这是一种获取在DF-B中出现的行中不出现在DF-B2中的行的方法。

使用 ~进行蒙版倒数:

df-B[~df-B.e]

使用DataFrame.isin的解决方案更一般,因为它也检查索引和列值。因此,Y列设置为索引,然后获取掩码。最后使用boolean indexing

print (df1)
    B   Y
0  2  10
1  4  15
2  6  15
print (df2)
   B   Y     e
0  2  10  True
2  6  15  True
df11 = df1.set_index('B')[['Y']]
df22 = df2.set_index('B')[['Y']]
mask = df11.isin(df22).reset_index()
print (mask)
   B      Y
0  2   True
1  4  False
2  6   True
print (df1[mask.Y])
   B   Y
0  2  10
2  6  15
print (df1[~mask.Y])
   B   Y
1  4  15

最新更新