[英]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 ![]() |
Item![]() |
Qty![]() |
---|---|---|
123 ![]() |
qwe ![]() |
1 ![]() |
123 ![]() |
asd![]() |
4 ![]() |
123 ![]() |
zxc![]() |
7 ![]() |
234 ![]() |
ewr![]() |
2 ![]() |
234 ![]() |
sdf![]() |
5 ![]() |
345 ![]() |
xcv ![]() |
8 ![]() |
345 ![]() |
qwe ![]() |
3 ![]() |
Second DF2:( 6 x 3)第二个 DF2:( 6 x 3)
ID ![]() |
Item![]() |
Qty![]() |
---|---|---|
123 ![]() |
asd![]() |
3 ![]() |
123 ![]() |
qwe ![]() |
6 ![]() |
234 ![]() |
ewr![]() |
9 ![]() |
234 ![]() |
sdf![]() |
2 ![]() |
345 ![]() |
qwe ![]() |
5 ![]() |
345 ![]() |
xcv ![]() |
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 ![]() |
Item![]() |
Qty![]() |
---|---|---|
123 ![]() |
qwe ![]() |
-5 ![]() |
123 ![]() |
asd![]() |
1 ![]() |
123 ![]() |
zxc![]() |
7 ![]() |
234 ![]() |
ewr![]() |
-7 ![]() |
234 ![]() |
sdf![]() |
3 ![]() |
345 ![]() |
xcv ![]() |
0 ![]() |
345 ![]() |
qwe ![]() |
-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.