简体   繁体   中英

How to Compare Two Columns From Two Dataframes for Differences?

I have two DataFrames, and I need to compare two columns in my first DataFrame with two columns in another DataFrame to compare the differences in the values.

This is what my first DataFrame looks like:

item_number sell_price
50 12
50 12
43 15
21 20
66 54
66 102
66 76

This is what my second DataFrame looks like:

item_number price
50 15
50 15
43 15
21 28
66 87
66 87
66 78

Now, how do I compare the item_number and sell_price in my first DataFrame with the item_number and price in my second DataFrame?

I need to see the differences between the two DataFrames for the desired columns.

I am looking for an output like this:

item_number sell_price price
50 12 15
50 12 15
21 20 28
66 54 87
66 102 87
66 76 78

Here's an example:

import pandas as pd
df1=pd.DataFrame({'item_number':[10,20],'sell_price':[20,40]},index=[0,1])
df2=pd.DataFrame({'item_number':[10,20],'price':[15,20]},index=[0,1])
df1['price']=df2['price']

Note you're effectively adding a column to the original df1. You can always reassign to another df if you wish.

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