I have two dataframes with unique ids and matching columns like these:
df1 = pd.DataFrame({"ID" : [1,2,3], "Col1": [12,7,4], "Col2": [1,7,2]})
ID Col1 Col2
1 12 1
2 7 7
3 4 2
df2 = pd.DataFrame({"ID" : [2,3,5], "Col1": [3,4,3], "Col2": [5,5,2], "Col3": [0,11,3]})
ID Col1 Col2 Col3
2 3 5 0
3 4 5 11
5 3 2 3
What I would like to do is merge those dataframes on the ID column and sum the matching ones. The not matching columns should remain as they are.
It should become something like this
ID Col1 Col2 Col3
1 12 1 NaN
2 10 12 0
3 8 7 11
5 3 2 3
I tried adding the one df to the other, but it doesn't work.
Use concat, groupby and sum i.e
ndf = pd.concat([df1,df2]).groupby('ID').sum()
Col1 Col2 Col3
ID
1 12 1 NaN
2 10 12 0.0
3 8 7 11.0
5 3 2 3.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With