1
Df1 

 A B C
 1  1 'a'
 2  3 'b'
 3  4 'c'

Df2
  A  B  C
  1  1 'k'
  5  4 'e'

Expected output (after difference and merge of Df1 and Df2) i.e. Df1-Df2 and then merge

output 

     A B C
     1  1 'a'
     2  3 'b'
     3  4 'c'
     5  4 'e'

The difference should be based on two columns A and B and not all three columns. I do not care what column C contains in both Df2 and Df1.

6
  • 1
    Why should the new dataframe have 1,1,"a" and not 1,1,"k"? Is there a criteria for column C? Should column C be dropped? Commented Jul 1, 2016 at 15:22
  • It is like set difference. Left operand values should be there. Commented Jul 1, 2016 at 15:24
  • why is 5, 4, 'e' there? Commented Jul 1, 2016 at 15:28
  • it is difference and merge. Commented Jul 1, 2016 at 15:29
  • Yes, but you need a criteria for handling column C, or you need to drop it. Should it always be the first dataframe's column C value? The seconds? Randomly pick? Commented Jul 1, 2016 at 15:31

3 Answers 3

2

try this:

In [44]: df1.set_index(['A','B']).combine_first(df2.set_index(['A','B'])).reset_index()
Out[44]:
   A  B    C
0  1  1  'a'
1  2  3  'b'
2  3  4  'c'
3  5  4  'e'
Sign up to request clarification or add additional context in comments.

2 Comments

Kudos to you for figuring out what the OP was thinking. But isn't this more like set addition? +1
@piRSquared, yes, I think you are right. But let's see whether it's OK for OP or not.
0

It's an outer join, then merging in column C from df2 if a value is not known in df1:

dfx = df1.merge(df2, how='outer', on=['A', 'B'])
dfx['C'] = dfx.apply(
     lambda r: r.C_x if not pd.isnull(r.C_x) else r.C_y, axis=1)
dfx[['A', 'B', 'C']]
=>
   A  B  C
0  1  1  a
1  2  3  b
2  3  4  c
3  5  4  e

Comments

0

Using concat and drop_duplicates:

output = pd.concat([df1, df2])
output = output.drop_duplicates(subset = ["A", "B"], keep = 'first')

* Desired df: *

   A  B  C
0  1  1  a
1  2  3  b
2  3  4  c
1  5  4  e

Comments

Your Answer

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