Reputation: 39
I have two dataframes A and B
A = pd.DataFrame({'a'=[1,2,3,4,5], 'b'=[11,22,33,44,55]})
B = pd.DataFrame({'a'=[7,2,3,4,9], 'b'=[123,234,456,789,1122]})
I want to merge B and A such that I don't want the common values in column 'a' in A and B from B, only non-intersecting values from B in column 'a' should be taken. The final dataframe should look like
a | b |
---|---|
1 | 11 |
2 | 22 |
3 | 33 |
4 | 44 |
5 | 55 |
7 | 123 |
9 | 1122 |
Upvotes: 1
Views: 168
Reputation: 150825
If a
is unique-valued in both A
and B
(some sort of unique ID for example), you can try with concat
and drop_duplicates
:
pd.concat([A,B]).drop_duplicates('a')
Output:
a b
0 1 11
1 2 22
2 3 33
3 4 44
4 5 55
0 7 123
4 9 1122
In the general case, use isin
to check for existence of B['a']
in A['a']
:
pd.concat([A,B[~B['a'].isin(A['a'])])
Upvotes: 2