-1

I have 2 dataframes.

One is

DF1

col1   col2   label
  a      qq       1
  a      ww       1
  a      ee       0
  b      qq       1
  b      ww       0
  b      rr       0

Another one is

DF2

col1   col2   label
  a      qq
  a      ww
  a      ee
  b      qq
  b      tt
  b      rr

So if there is matching pair with col1 and col2, I want to write label of DF1 to DF2 label. And if there is no matching pair, I want to just mark 0 to DF2 label.

How can i deal with this.

Thanks in advance.

puhuk
  • 464
  • 5
  • 15

1 Answers1

1

Could you please try following.

df2['label']=np.where(df2['label'].isna(),df1['label'],df2['label'])

Once we print edited df2 its values will be as follows now.

    col1   col2 label
0   a       qq  1.0
1   a       ww  1.0
2   a       ee  0.0
3   b       qq  1.0
4   b       tt  0.0
5   b       rr  0.0
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • it says operands could not be broadcast together with shapes (2435,) (2435,) (8828,) – puhuk Dec 11 '19 at 08:33
  • 1
    @puhuk, not sure but for your given samples it worked fine. I created 2 df from your provided samples and ran above code which executed from me for me. – RavinderSingh13 Dec 11 '19 at 08:42