2

I have two data frame

First 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)

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. And repeat the same for other ID's

where new column is

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

Result required : (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

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'

Please suggest.

2
  • 2
    merge them then calculate difference see pandas-merging-101 Commented Jul 27, 2021 at 16:13
  • 1
    Merge on ID and item, then qty_x - qty_y Commented Jul 27, 2021 at 16:14

1 Answer 1

1

here you go, merge on id and 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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.