After mapping by column c, If column A has a value, insert the value of column A; if not, insert column B.
data1 data2
a b c a c d
a1 b1 c1 1a c1 1d
b2 c2 2a c2 2d
a3 c3 3a c3 3d
4a c4 4d
The result I want
result
a b c
a1 b1 c1
2a b2 c2
a3 c3
I tried the following, but I was not satisfied.
->>> result = data1.merge(data2, on=['c'])
Prefixes _x and _y are created. combine_first is not applied.
->>> result = data1.combine_first(data2)
It is not mapped by column c.
How do I get good results? I ask for your help. thank you
Using @IdoS setup:
You can use
set_index
,combine_first
, and reindex:Output: