I am working with two dataframes that are very similar, and I'm trying to figure out how to get the data that is in one and not the other - and visa versa.
Here is my code so far:
import pandas as pd
import numpy as np
def report_diff(x):
return x[0] if x[0] == x[1] else '{} ---> {}'.format(*x)
old = pd.read_excel('File 1')
new = pd.read_excel('File 2')
old['version'] = 'old'
new['version'] = 'new'
full_set = pd.concat([old,new],ignore_index=True)
changes = full_set.drop_duplicates(subset=['ID','Type', 'Total'], keep='last')
duplicated = changes.duplicated(subset=['ID', 'Type'], keep=False)
dupe_accts = changes[duplicated]
change_new = dupe_accts[(dupe_accts['version'] == 'new')]
change_old = dupe_accts[(dupe_accts['version'] == 'old' )]
change_new = change_new.drop(['version'], axis=1)
change_old = change_old.drop(['version'],axis=1)
change_new.set_index('Employee ID', inplace=True)
change_old.set_index('Employee ID', inplace=True)
diff_panel = pd.Panel(dict(df1=change_old,df2=change_new))
diff_output = diff_panel.apply(report_diff, axis=0)
And so the next step would be to get the data that is only in old and only in new.
My first attempt was:
changes['duplicate']=changes['Employee ID'].isin(dupe_accts)
removed_accounts = changes[(changes['duplicate'] == False) & (changes['version'] =='old')]