简体   繁体   English

如何比较两个不同数据框的两列并添加新的结果列

[英]How can I compare two columns of two different data frame and add new resultant column

I have two data frame我有两个数据框

First DF1: ( 7 x 3)第一个 DF1:( 7 x 3)

ID ID Item物品 Qty数量
123 123 qwe qwe 1 1
123 123 asd阿斯达 4 4
123 123 zxc 7 7
234 234 ewr厄尔 2 2
234 234 sdf自卫队 5 5
345 345 xcv xcv 8 8
345 345 qwe qwe 3 3

Second DF2:( 6 x 3)第二个 DF2:( 6 x 3)

ID ID Item物品 Qty数量
123 123 asd阿斯达 3 3
123 123 qwe qwe 6 6
234 234 ewr厄尔 9 9
234 234 sdf自卫队 2 2
345 345 qwe qwe 5 5
345 345 xcv xcv 8 8

I want to compare 123 ID of DF1 & DF2 and in that id compare Qty of items for DF1 and DF2 and get a new column.我想比较 DF1 和 DF2 的 123 个 ID,并在该 ID 中比较 DF1 和 DF2 的项目数量并获得一个新列。 And repeat the same for other ID's并对其他 ID 重复相同的操作

where new column is新列在哪里

DF1['Qty_new']= DF1['Qty'] - DF2['Qty']

Result required : (7 x 3)结果要求:(7 x 3)

ID ID Item物品 Qty数量
123 123 qwe qwe -5 -5
123 123 asd阿斯达 1 1
123 123 zxc 7 7
234 234 ewr厄尔 -7 -7
234 234 sdf自卫队 3 3
345 345 xcv xcv 0 0
345 345 qwe qwe -2 -2

I've tried using我试过使用

if (DF1['ID'] == DF2['ID']):
 while (DF1['Item'] == DF2['Item']):
  DF1['Qty_new']= DF1['Qty'] - DF2['Qty']

Getting error as: ValueError: Can only compare identically-labeled Series objects获取错误为: ValueError: Can only compare identically-labeled Series objects

Also tried也试过

while (DF1['ID'] == DF2['ID']) & (DF1['Item'] == DF2['Item']):
 DF1['Qty_new']= DF1['Qty'] - DF2['Qty']

Error TypeError: unsupported operand type(s) for &: 'str' and 'str'错误TypeError: unsupported operand type(s) for &: 'str' and 'str'错误TypeError: unsupported operand type(s) for &: 'str' and 'str'

Please suggest.请建议。

here you go, merge on id and item:在这里,合并 id 和 item:

comb = pd.merge(DF1, DF2, on=['ID', 'Item'], how='left').rename(columns={'Qty_x': 'DF1_Qty','Qty_y': 'DF2_Qty'})
comb = comb.fillna(0)
comb['Qty_new'] = comb['DF1_Qty'] - comb['DF2_Qty']

暂无
暂无

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

相关问题 使用两个条件比较来自两个不同数据框的两列 - Compare two columns from two different data frame with two conditions 我如何比较python中两行不同的两列 - How can i compare two columns in two different rows in python 如何合并不同数据框的两列,如果找到匹配项,请使用 pandas 在新列中写入“True” - How to merge two columns of different data frame and if the match is found write "True" in a new column using pandas 比较两个熊猫数据框列的元素,并基于第三列创建一个新列 - Compare elements of two pandas data frame columns and create a new column based on a third column 如何根据条件比较两列并分别打印包含(两列的)任一值的新列? - How can I compare two columns and print a new column containing either of the values (of the two columns) respectively according to a condition? 比较两个不同数据框的两列,并使用If条件创建新列 - Comparing Two columns of Two different data frame and create new column with If condition 如何比较 python 数据框中两列中的标记词 - How can I compare tokenise word in two column in python data frame 如何比较两个不同数据框中的列并保留第一个数据框中的值? - How to compare columns from two different Data Frames and keep the values from the first Data Frame? 如何将数据框中的两个列添加到np.array - How can I add two of my columns in my data frame to an np.array 当两个 Null 值存在于同一行但在不同的列中时,如何将两个列名获取到新列 - when two Null values are existing in same row but in different columns , how can i get the two column names to a new column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM