Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sales2.where(~(sales1==sales2))
- ###############################################################################
- # solution after concatenation:
- sales_comp = pd.concat([sales1,sales2], axis = 1, keys = ['Day1','Day2'])
- def highlight_diff(data, color='yellow'):
- attr = 'background-color: {}'.format(color)
- other = data.xs('Day1', axis='columns', level=-2)
- return pd.DataFrame(np.where(data.ne(other, level=1), attr, ''),
- index=data.index, columns=data.columns)
- sales_comp.style.apply(highlight_diff, axis=None)
- ############################################################################
- 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) & ~(df1.isnull() & df2.isnull())
- ne_stacked = diff_mask.stack()
- changed = ne_stacked[ne_stacked]
- changed.index.names = ['id', 'col']
- difference_locations = np.where(diff_mask)
- changed_from = df1.values[difference_locations]
- changed_to = df2.values[difference_locations]
- return pd.DataFrame({'from': changed_from, 'to': changed_to},
- index=changed.index)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement