[英]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 ![]() |
12 ![]() |
50 ![]() |
12 ![]() |
43 ![]() |
15 ![]() |
21 ![]() |
20 ![]() |
66 ![]() |
54 ![]() |
66 ![]() |
102 ![]() |
66 ![]() |
76 ![]() |
This is what my second DataFrame looks like:这是我的第二个 DataFrame 的样子:
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?现在,如何将我的第一个 DataFrame 中的
item_number
和sell_price
与我的第二个 DataFrame 中的item_number
和price
进行比较?
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 ![]() |
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.请注意,您实际上是在向原始 df1 添加一列。 You can always reassign to another df if you wish.
如果您愿意,您可以随时重新分配给另一个 df。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.