Pandas DataFrame merge summing column

This solution works also if you want to sum more than one column. Assume data frames

>>> df1
   id name  weight  height
0   1    A       0       5
1   2    B      10      10
2   3    C      10      15
>>> df2
   id name  weight  height
0   2    B      25      20
1   3    C      20      30

You can concatenate them and group by index columns.

>>> pd.concat([df1, df2]).groupby(['id', 'name']).sum().reset_index()
   id name  weight  height
0   1    A       0       5
1   2    B      35      30
2   3    C      30      45

Leave a Comment