Here is a straightforward method (you can reset_index()
at the end if you want)
Create with a multi-index on id1 and id2
In [24]: df1 = DataFrame([['ID1','ID2',1],['ID1','ID3',1],['ID2','ID4',1]],columns=['id1','id2','value']).set_index(['id1','id2'])
In [25]: df2 = DataFrame([['ID1','ID2',1],['ID1','ID3',1],['ID1','ID4',1]],columns=['id1','id2','value']).set_index(['id1','id2'])
In [26]: df1
Out[26]:
value
id1 id2
ID1 ID2 1
ID3 1
ID2 ID4 1
In [27]: df2
Out[27]:
value
id1 id2
ID1 ID2 1
ID3 1
ID4 1
Join the index
In [35]: joined_index = df1.index+df2.index
Reindex both by the joint index, fill with 0 and add
In [36]: df1.reindex(joined_index,fill_value=0) + df2.reindex(joined_index,fill_value=0)
Out[36]:
value
id1 id2
ID1 ID2 2
ID3 2
ID4 1
ID2 ID4 1
Here is another way (and allows various ways of joining if you specify join
kw)
In [41]: a1, a2 = df1.align(df2, fill_value=0)
In [42]: a1 + a2
Out[42]:
value
id1 id2
ID1 ID2 2
ID3 2
ID4 1
ID2 ID4 1