df1
fileName obj1 obj2 obj3 obj4
file_01.jpg 1 1 1
file_02.jpg 1 1
file_03.jpg 1 2 1
df2
fileName obj1 obj2 obj3 obj4
file_01.jpg 1 2
file_02.jpg 1 1
file_04.jpg 3 1 2
Say I have two pandas DataFrames like above, question: how can I calculate the difference between te two to get a final DataFrame like below?
Expected output
resultdf
fileName obj1 obj2 obj3 obj4
file_01.jpg 0 1 -1 0
file_02.jpg 0 0 0 0
file_03.jpg 1 2 0 1
file_04.jpg 0 -3 -1 -2
What I've already tried : I reached the result by merging the two DataFrames together, and then calculating the difference between two columns. Raising this question in SO to see if there is any other efficient method.
I can't seem to find a groupby subtract so I multiplied your second df by -1 so sum could product the desired result
obj_cols = ['obj1', 'obj2', 'obj3', 'obj4']
df2[obj_cols] *= -1
pd.concat([df1, df2]).groupby(['fileName'], as_index=False).sum()
fileName obj1 obj2 obj3 obj4
0 file_01.jpg 0 1 -1 0
1 file_02.jpg 0 0 0 0
2 file_03.jpg 1 2 0 1
3 file_04.jpg 0 -3 -1 -2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With