简体   繁体   English

如何比较两个数据框中的两列是否存在差异?

[英]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.我有两个 DataFrame,我需要将第一个 DataFrame 中的两列与另一个 DataFrame 中的两列进行比较,以比较值的差异。

This is what my first DataFrame looks like:这是我的第一个 DataFrame 的样子:

item_number项目编号 sell_price卖价
50 50 12 12
50 50 12 12
43 43 15 15
21 21 20 20
66 66 54 54
66 66 102 102
66 66 76 76

This is what my second DataFrame looks like:这是我的第二个 DataFrame 的样子:

item_number项目编号 price价格
50 50 15 15
50 50 15 15
43 43 15 15
21 21 28 28
66 66 87 87
66 66 87 87
66 66 78 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?现在,如何将我的第一个 DataFrame 中的item_numbersell_price与我的第二个 DataFrame 中的item_numberprice进行比较?

I need to see the differences between the two DataFrames for the desired columns.我需要查看所需列的两个 DataFrame 之间的差异。

I am looking for an output like this:我正在寻找这样的 output:

item_number项目编号 sell_price卖价 price价格
50 50 12 12 15 15
50 50 12 12 15 15
21 21 20 20 28 28
66 66 54 54 87 87
66 66 102 102 87 87
66 66 76 76 78 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.请注意,您实际上是在向原始 df1 添加一列。 You can always reassign to another df if you wish.如果您愿意,您可以随时重新分配给另一个 df。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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