Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregating two Pandas series by summing [duplicate]

I am trying to sum two series that have some matching indexes, but some that are unique. e.g.:

a = pd.Series([0.2, 0.1, 0.3], index=['A', 'B', 'C'])
b = pd.Series([0.2, 0.2], index=['A', 'D'])

Notice that index A is in both a, and b. I want to end up with a new series, which has the summed up aggregate of all indices:

A    0.4
B    0.1
C    0.3
D    0.2
dtype: float64

notice index A is the sum of both a and b (0.2 + 0.2), whereas B, C, and D are the original value. If I try to do:

c = a + b

I get the proper value for index A, but NaN for all other values. Any thoughts on the best way to do this?

like image 881
MarkD Avatar asked Feb 19 '15 16:02

MarkD


1 Answers

c = a.add(b, fill_value=0)
In [28]: c
Out[28]: 
A    0.4
B    0.1
C    0.3
D    0.2
dtype: float64 

Use the .add method.

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.Series.add.html#pandas.Series.add

Adding two pandas.series objects

like image 189
Liam Foley Avatar answered Nov 15 '22 00:11

Liam Foley