Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas merge two dataframes summing values [duplicate]

Tags:

python

pandas

Suppose I have two dataframes with partly repeated entries:

source1=pandas.DataFrame({'key':['a','b'],'value':[1,2]})
#  key  value
#0   a      1
#1   b      2
source2=pandas.DataFrame({'key':['b','c'],'value':[3,0]})
#  key  value
#0   b      3
#1   c      0

What do I need to do with source1 and source2 in order to get resulting frame with following entries:

#  key  value
#0   a      1
#1   b      5
#2   c      0
like image 402
Dimitry Avatar asked Mar 06 '26 12:03

Dimitry


1 Answers

Just add

source1.set_index('key').add(source2.set_index('key'), fill_value=0)

If key is already the index, just use

source1.add(source2, fill_value=0)

You man want to .reset_index() at the end if you don't want key as index

like image 133
rafaelc Avatar answered Mar 09 '26 00:03

rafaelc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!