Home > OS >  XOR operation on 2 pandas DataFrames
XOR operation on 2 pandas DataFrames

Time:03-08

Is there any way to remove from first DataFrame all rows which can be found in second DataFrame and add rows which are exclusive only in second DataFrame (= XOR)? Here's a twist: the first DataFrame has one column that shall be ignored during comparison.

import pandas as pd

df1 = pd.DataFrame({'col1': [1,2,3],
                   'col2': [4,5,6],
                   'spec': ['A','B','C']})

df2 = pd.DataFrame({'col1': [1,9],
                   'col2': [4,9]}) 


result = pd.DataFrame({'col1': [2,3,9],
                   'col2': [5,6,9],
                   'spec': ['B','C','df2']})

df1 = df1.astype(str) 
df2 = df1.astype(str)

This is analogical to UNION (not UNION ALL) operation.

Combine

   col1  col2 spec
0     1     4    A
1     2     5    B
2     3     6    C

and

   col1  col2
0     1     4
1     9     9

to

   col1  col2 spec
1     2     5    B
2     3     6    C
1     9     9  df2

CodePudding user response:

You could concatenate and drop duplicates:

out = (pd.concat((df1, df2.assign(spec='df2')))
       .drop_duplicates(subset=['col1','col2'], keep=False))

or filter out the common rows and concatenate:

out = pd.concat((df1[~df1[['col1','col2']].isin(df2[['col1','col2']]).all(axis=1)], 
                 df2[~df2.isin(df1[['col1','col2']]).all(axis=1)].assign(spec='df2')))

Output:

   col1  col2 spec
1     2     5    B
2     3     6    C
1     9     9  df2
  • Related