how to find the complement of two dataframes
<p>given two large dataframes, is there any concise and efficient code (avoid using any <code>for loop</code> directly) that allow me to obtain the complement of these two dataframes? </p>

<p>the most straight forward way to me is to compute <code>union-intersection</code> as shown in the naive example below, but I do not know how to implement this in an elegant languages of <code>pandas</code> or <code>np</code></p>

<pre><code>df1= pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
                     'key2': ['K0', 'K1', 'K0', 'K1'],
                   'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3']})     
df2= pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
                      'key2': ['K0', 'K0', 'K0', 'K0'],
                      'C': ['C0', 'C1', 'C2', 'C3'],
                      'D': ['D0', 'D1', 'D2', 'D3']})        
intersection= pd.merge(df1, df2, how='inner',on=['key1', 'key2'])
union=pd.merge(df1, df2, how='outer',on=['key1', 'key2'])       


complement=union-intersection
</code></pre>

<p>thanks for any comments and answers</p>