pandas 熊猫左连接,其中多列上的右为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49487263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:21:57  来源:igfitidea点击:

pandas left join where right is null on multiple columns

python-3.xpandas

提问by edoedoedo

I have two pandas df x and y, both with the same 3 columns A B C (not nullable). I need to create a new df z, obtained by "subtracting from x the rows which are entirely identical to the rows of y", i.e. a

我有两只Pandas df x 和 y,它们都具有相同的 3 列 ABC(不可为空)。我需要创建一个新的 df z,通过“从 x 中减去与 y 的行完全相同的行”获得,即

x left join y on x.A=y.A and x.B=y.B and x.C=y.C
where y.A is null

How would I do that? Got stuck with indexes, concat, merge, join, ...

我该怎么做?被索引、连接、合并、连接、......

Example:

例子:

dataframe x
A    B    C
q1   q2   q3
q4   q2   q3
q7   q2   q9

dataframe y
A    B    C
q4   q2   q3

dataframe z
A    B    C
q1   q2   q3
q7   q2   q9

回答by jezrael

I think need mergewith indicator and filter only rows from leftDataFrame:

我认为需要merge使用指标并仅从以下位置过滤行leftDataFrame

df = x.merge(y, indicator='i', how='outer').query('i == "left_only"').drop('i', 1)
print (df)
    A   B    C
0  q1  q2   q3
2  q7  q2  q93