Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two DataFrames and aggregate matching columns

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.

like image 320
1nfern0 Avatar asked Feb 05 '26 16:02

1nfern0


1 Answers

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
like image 174
Bharath Avatar answered Feb 07 '26 06:02

Bharath