简体   繁体   中英

Compare column values of two dataframes using pandas

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(ie 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

df1: 在此处输入图像描述

df2: 在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM