“反合并"在 pandas (Python) [英] "Anti-merge" in pandas (Python)
问题描述
如何找出两个数据框中同名列之间的区别?我的意思是我有一个名为 X 的列的数据框 A 和名为 X 的列的数据框 B,如果我执行 pd.merge(A, B, on=['X'])
,我会得到A 和 B 的共同 X 值,但我怎样才能得到非共同"的值?
How can I pick out the difference between to columns of the same name in two dataframes?
I mean I have dataframe A with a column named X and dataframe B with column named X, if i do pd.merge(A, B, on=['X'])
, i'll get the common X values of A and B, but how can i get the "non-common" ones?
推荐答案
如果您将合并类型更改为 how='outer'
和 indicator=True
这将添加一列告诉您值是否为左/双/右:
If you change the merge type to how='outer'
and indicator=True
this will add a column to tell you whether the values are left/both/right only:
In [2]:
A = pd.DataFrame({'x':np.arange(5)})
B = pd.DataFrame({'x':np.arange(3,8)})
print(A)
print(B)
x
0 0
1 1
2 2
3 3
4 4
x
0 3
1 4
2 5
3 6
4 7
In [3]:
pd.merge(A,B, how='outer', indicator=True)
Out[3]:
x _merge
0 0.0 left_only
1 1.0 left_only
2 2.0 left_only
3 3.0 both
4 4.0 both
5 5.0 right_only
6 6.0 right_only
7 7.0 right_only
然后您可以在 _merge
列上过滤结果合并的 df:
You can then filter the resultant merged df on the _merge
col:
In [4]:
merged = pd.merge(A,B, how='outer', indicator=True)
merged[merged['_merge'] == 'left_only']
Out[4]:
x _merge
0 0.0 left_only
1 1.0 left_only
2 2.0 left_only
您也可以使用 isin
并否定掩码以查找不在 B 中的值:
You can also use isin
and negate the mask to find values not in B:
In [5]:
A[~A['x'].isin(B['x'])]
Out[5]:
x
0 0
1 1
2 2
这篇关于“反合并"在 pandas (Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!