Reputation: 987
I would like to add series a and b together. I know that the keys in series b are always in a, but there will be keys in a that are not in b. I don't want nans to appear in the result.
For example:
In[114] a
Out[114]:
a 1
b 3
c 2
d 5
dtype: int64
In[115] b
Out[115]:
b 3
c 2
dtype: int64
If I just use the add function, I will get nans in the missing locations.
In[116] a.add(b)
Out[116]:
a NaN
b 6
c 4
d NaN
dtype: float64
The following is the result I desire:
In[117] c
Out[117]:
a 1
b 6
c 4
d 5
dtype: int64
Is there a clever way to do this?
Upvotes: 0
Views: 91
Reputation: 26
use update and nested add, this should give you the desired output. I have tested it in ipython and it works.
d = {'d': 5, 'b': 3, 'c': 2, 'a': 1}
e = {'b': 3, 'c': 2}
Convert to series
ds=pd.Series(d)
es=pd.Series(e)
**ds.update(ds.add(es))**
ds
Out[49]:
a 1
b 6
c 4
d 5
dtype: int64
Upvotes: 1