I'm sure this question must have already been answered somewhere but I couldn't find an answer that suits my case.
I have 2 pandas DataFrames
a = pd.DataFrame({'A1':[1,2,3], 'A2':[2,4,6]}, index=['a','b','c'])
b = pd.DataFrame({'A1':[3,5,6], 'A2':[3,6,9]}, index=['a','c','d'])
我想合并它们以获得类似的结果
result = pd.DataFrame({
'A1' : [3,2,5,6],
'A2' : [3,4,6,9]
}, index=['a','b','c','d'])
Basically, I want a new df with the union of both indexes. Where indexes match, the value in each column should be updated with the one from the second df (in this case b
). Where there is no match the value is taken from the starting df (in this case a
).
I tried with merge(), join() and concat() but I could not manage to obtain this result.