pandas 比较两个数据框并在一列中删除匹配的内容 [英] Pandas compare two dataframes and remove what matches in one column

查看:61
本文介绍了 pandas 比较两个数据框并在一列中删除匹配的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单独的熊猫数据帧(df1df2),它们有多列,但只有一个共同的列(文本").

I have two separate pandas dataframes (df1 and df2) which have multiple columns, but only one in common ('text').

我想在df2df1共有的列的任何行中找到df2中没有匹配项的每一行.

I would like to do find every row in df2 that does not have a match in any of the rows of the column that df2 and df1 have in common.

df1

A    B    text
45   2    score
33   5    miss
20   1    score

df2

C    D    text
.5   2    shot
.3   2    shot
.3   1    miss

结果df(由于它发生在df1中,因此删除了包含未命中的行)

Result df (remove row containing miss since it occurs in df1)

C    D    text
.5   2    shot
.3   2    shot

在这种情况下是否可以使用isin方法?

Is it possible to use the isin method in this scenario?

推荐答案

根据您的要求,您可以使用isin有效地做到这一点(而无需求助于昂贵的merge s).

As you asked, you can do this efficiently using isin (without resorting to expensive merges).

>>> df2[~df2.text.isin(df1.text.values)]
C   D   text
0   0.5 2   shot
1   0.3 2   shot

这篇关于 pandas 比较两个数据框并在一列中删除匹配的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆