Reputation: 133
I am new to python so please go easy. I have been looking up similar threads but unable to find a neat solution for my problem:
I want to compare column values of two data frames and put the values where different in a new data frame my data frames are below and are different in lenghth(i.e. number of rows). I want to compare Status for each SKU:
df1
SKU PRICE Status
A 1798 0
C 1798 1
D 999 0
E 1299 1
F 999 0
df2
SKU PRICE Status
A 1798 1
B 2997 1
C 1798 1
D 999 0
Comparing df2 with df1 I am want to get following df3
SKU PRICE Status
A 1798 0
I know it can be done via loops but I am hoping there is a better solution via pandas or itertools out there?
Thanks for your help
Upvotes: 1
Views: 75
Reputation: 403012
This is a simple merge and filtering operation:
df1.merge(df2, on=['SKU', 'PRICE']).query('Status_x != Status_y')
SKU PRICE Status_x Status_y
0 A 1798 0 1
Or, more accurately:
(df1.merge(df2, on=['SKU', 'PRICE'], suffixes=('', '_y'))
.query('Status != Status_y')
.drop('Status_y', 1))
SKU PRICE Status
0 A 1798 0
Upvotes: 1