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
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
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