简体   繁体   中英

Python: Pandas compare two dataframes and get the different rows

compare two dataframes and get the different rows

have two df's

name    password   login_id
tom      1234         1
matt     4567         2
george   7469         
stuart                3
name    password   login_id
tom      1234         1
matt     7859         2
george   7469         5
stuart   4682         3

Need to get the changed value complete rows

expected output 

name    password   login_id
matt     7859         2
george   7469         5
stuart   4682         3

Need to get only rows which has changed in second df

You can use df.ne after setting name as the index in both the dataframes and use df.any over axis 1.

mask = df.set_index('name').ne(df1.set_index('name')).any(1).tolist()
df1[mask]
     name  password  login_id
1    matt      7859         2
2  george      7469         5
3  stuart      4682         3

You can try merge with indicator

df2.merge(df1,how='left',indicator=True).loc[lambda x : x['_merge']=='left_only']
     name  password  login_id     _merge
1    matt      7859         2  left_only
2  george      7469         5  left_only
3  stuart      4682         3  left_only

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