title: Comparing two Data Frames and getting differences
body: <p>I want to compare two Data Frames and print out my differences in a selective way. Here is what I want to accomplish in pictures:</p>
  
  <p>Dataframe 1</p>
  
  <p><img src="https://i.stack.imgur.com/rsr2v.png" alt="Dataframe 1"></p>
  
  <p>Dataframe 2</p>
  
  <p><img src="https://i.stack.imgur.com/TKHA3.png" alt="Dataframe 2"></p>
  
  <p>Desired Output - Dataframe 3</p>
  
  <p><img src="https://i.stack.imgur.com/pFJGX.png" alt="Desired Output - Dataframe 3"></p>
  
  <p>What I have tried so far?</p>
  
  <pre class="lang-html prettyprint-override"><code>import pandas as pd
  import numpy as np
  
  df1 = pd.read_excel("01.xlsx")
  df2 = pd.read_excel("02.xlsx")
  
  def diff_pd(df1, df2):
      """Identify differences between two pandas DataFrames"""
      assert (df1.columns == df2.columns).all(), \
          "DataFrame column names are different"
      if any(df1.dtypes != df2.dtypes):
         "Data Types are different, trying to convert"
          df2 = df2.astype(df1.dtypes)
      if df1.equals(df2):
          return None
      else:        # need to account for np.nan != np.nan returning True
          diff_mask = (df1 != df2) &amp; ~(df1.isnull() &amp; df2.isnull())
          ne_stacked = diff_mask.stack()
          changed = ne_stacked[ne_stacked]
          changed.index.names = ['id', 'Naziv usluge']
          difference_locations = np.where(diff_mask)
         changed_from = df1.values[difference_locations]
          changed_to = df2.values[difference_locations]
      return pd.DataFrame({'Service Previous': changed_from, 'Service Current': changed_to},
                              index=changed.index)
  
  df3 = diff_pd(df1, df2)
  
  df3 = df3.fillna(0)
  df3 = df3.reset_index()
  
  print(df3)
  </code></pre>
  
  
  
  <p>To be fair i found that code on another thread, but it does get job done, but I still have some issues.</p>
  
  <ol>
  <li>My dataframes are not equal, what do I do?</li>
  <li>I don't fully understand the code I provided.</li>
  </ol>
  
  <p>Thank you!</p>