Night Walker
Night Walker

Reputation: 21280

Merging DataFrames and staying only with not matching entries

I have two DataFrames:

dfA = pd.DataFrame([['A', 'B', 'C', 'D'], ['A1', 'B1', 'C1', 'D1'], ['A2', 'B2', 'C2', 'D2'], ['A', 'B3', 'C3', 'D3'], ['A', 'B', 'C4', 'D5']], columns = ['AA', 'BB', 'CC', 'DD'])

       AA  BB  CC  DD
    0   A   B   C   D
    1  A1  B1  C1  D1
    2  A2  B2  C2  D2
    3   A  B3  C3  D3
    4   A   B  C4  D5

dfB = pd.DataFrame([['A', 'B'], ['A', 'B3']], columns = ['AA', 'BB'])

  AA  BB
0  A   B
1  A  B3

I want to merge them on ['AA', 'BB'] columns and I want to stay only with not matching entries:

result = pd.DataFrame([['A1', 'B1', 'C1', 'D1'], ['A2', 'B2', 'C2', 'D2']], columns = ['AA', 'BB', 'CC', 'DD'])

   AA  BB  CC  DD
0  A1  B1  C1  D1
1  A2  B2  C2  D2

Thanks for help.

Upvotes: 1

Views: 43

Answers (1)

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

I think you are looking for this , merging and removing rows that has index that is merged.

dfA.drop(pd.merge(dfA,dfB,on=['AA','BB'],right_index=True).index)

Output:

   AA  BB  CC  DD
1  A1  B1  C1  D1
2  A2  B2  C2  D2

Upvotes: 2

Related Questions