title: Track Changes between two dataframes
body: <p>I need to track what changed between two dataframes:</p>
  
  <pre><code>df1 = DataFrame({'A':['a1','Changed','a3','Deleted'],'B':['b2','b2','b3','Deleted']})
  df2 = DataFrame({'A':['a1','a2','a3','Added'],'B':['b2','b2','Changed','Added']})
  </code></pre>
  
  <p>df1:</p>
  
  <pre><code>    A      B       df1
  0   a1      b2      1
  1   Changed b2      1
  2   a3      b3      1
  3   Deleted Deleted 1
  </code></pre>
  
  <p>df2:</p>
  
  <pre><code>    A       B      df2
  0   a1      b2      1
  1   a2      b2      1
  2   a3      Changed 1
  3   Added   Added   1
  </code></pre>
  
  <p>Desired Output:</p>
  
  <pre><code>A        B         df1  df2 Diff    Delta
  Added    Added      0   1   -1      Added
  Changed  b2         1   0    1      Changed
  a2       b2         0   1   -1      Added
  a3       Changed    1   0    1      Changed
  Deleted  Deleted    1   0    1      Deleted
  </code></pre>
  
  <p>Delas that need to be tracked are: Any changes to existing records, additions, deletions.</p>
  
  <p>I have tried initializing the presence of a row in each data frame with 1 and doing an outer merge, getting the Difference between 'df1' and 'df2' and using all the diff to design a logic that can assign changes, deletions, additions to a data frame. For example, Where diff == -1, it means that 'df1' = 0 and 'df2' = 1 implying that it was an addition.</p>
  
  <pre><code>df1['df1'] = 1
  df2['df2'] = 1
  df = pandas.merge(df1,df2,on=['A','B'],how='outer')
  df.fillna(0,inplace=True)
  df['Diff'] = df['df1'] - df['df2']
  df['Delta'] = numpy.nan
  df.loc[df['A'].duplicated(keep=False)&amp;df['Diff']!=0,'Delta'] ='Changed'
  df.loc[(df['Delta'].isnull())&amp;(df['Diff']== (-1)),'Delta'] = 'Added'
  df.loc[(df['Delta'].isnull())&amp;(df['Diff']== (1)),'Delta'] = 'Deleted'
  print df.loc[df['Delta'].notnull()].drop_duplicates(subset='A')
  </code></pre>
  
  <p>This gives me:</p>
  
  <p><a href="https://i.stack.imgur.com/tLtkB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tLtkB.png" alt="enter image description here"></a>
  I have added the column 'Correct_Delta' indicates what needs to be the correct output column 'Delta'. I have an issue with the highlighted cells.</p>
  
  <p>PS: column 'A' will always be unique and 'A' and 'B' will have many to one relationship in some cases  and one to one relationship in some cases.</p>
  
  <p>Can anybody please help?</p>