Advertisement
elena1234

identify the differences between dataframes in Python

Jan 6th, 2023 (edited)
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. sales2.where(~(sales1==sales2))
  2.  
  3. ###############################################################################
  4. # solution after concatenation:
  5. sales_comp = pd.concat([sales1,sales2], axis = 1, keys = ['Day1','Day2'])
  6.  
  7. def highlight_diff(data, color='yellow'):
  8.      attr = 'background-color: {}'.format(color)
  9.      other = data.xs('Day1', axis='columns', level=-2)
  10.      return pd.DataFrame(np.where(data.ne(other, level=1), attr, ''),
  11.                          index=data.index, columns=data.columns)
  12.      
  13. sales_comp.style.apply(highlight_diff, axis=None)
  14.  
  15.  
  16. ############################################################################
  17.     def diff_pd(df1, df2):
  18.         """Identify differences between two pandas DataFrames"""
  19.         assert (df1.columns == df2.columns).all(), \
  20.             "DataFrame column names are different"
  21.         if any(df1.dtypes != df2.dtypes):
  22.             "Data Types are different, trying to convert"
  23.             df2 = df2.astype(df1.dtypes)
  24.         if df1.equals(df2):
  25.             return None
  26.         else:
  27.             # need to account for np.nan != np.nan returning True
  28.             diff_mask = (df1 != df2) & ~(df1.isnull() & df2.isnull())
  29.             ne_stacked = diff_mask.stack()
  30.             changed = ne_stacked[ne_stacked]
  31.             changed.index.names = ['id', 'col']
  32.             difference_locations = np.where(diff_mask)
  33.             changed_from = df1.values[difference_locations]
  34.             changed_to = df2.values[difference_locations]
  35.             return pd.DataFrame({'from': changed_from, 'to': changed_to},
  36.                                 index=changed.index)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement