I am joining on two data sets on a column which has duplicated values in both datasets. Is it better practice to remove the duplicates and make the values I am joining on a primary key in both datasets before joining the two, or is it okay to first merge the two data sets, then make the joined column the primary key using something like .groupby()
?
E.g:
A = pd.DataFrame({'KEY' : ['abc', 'abc', '123', 'wyz'],
'WEIGHT' : [5, 7, 13, 10]
})
B = pd.DataFrame({'KEY': ['abc', '123', '123', 'def'],
'TITLE' : ['cat', 'dog', 'dog', 'elephant']
})
# join first then clean
C = pd.merge(A,B, how='inner', on='KEY')
C = C.groupby('KEY', as_index=False).agg(funcs) # mean for VALUE, first for TITLE
# versus clean then joining
A = A.groupby('KEY', as_index=False).mean()
B = B.groupby('KEY', as_index=False).first()
C = pd.merge(A,B, how='inner', on='KEY')
```