Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding pandas Series with different indices without getting NaNs

Tags:

python

pandas

I'm trying to do what I think is a straight froward operation in pandas but I can't seem to make it work.

I have two pandas Series with different numbers of indices, I would like to add values together if they share an index, otherwise I would just like to pass the values that don't have corresponding indices along.

For example

Sr1 = pd.Series([1,2,3,4], index = ['A', 'B', 'C', 'D']) Sr2 = pd.Series([5,6], index = ['A', 'C']) Sr1        Sr2 A     1    A     5 B     2    C     6 C     3 D     4 

Sr1 + Sr2 or Sr1.add(Sr2) give

A     6 B   NaN C     9 D   NaN 

But what I want is

A     6 B     2 C     9 D     4 

where the B and D values for Sr1 are just passed along.

Any suggestions?

like image 239
A Alstone Avatar asked Sep 20 '12 00:09

A Alstone


People also ask

Will there be NaN values if we combine add two Series with different indices?

When adding a Series to a DataFrame with a different index, the Series gets turned into all NaNs #450.

Does Pandas allow operations on Series of different indices?

If the operation is performed on series with different indexes, the result will contain the result of the operation on all entries whose index is contained in the union of the original indexes.

Can you change the index of a Pandas Series?

It is possible to specify or change the index labels of a pandas Series object after creation also. It can be done by using the index attribute of the pandas series constructor.


2 Answers

You could use fill_value:

>>> import pandas as pd >>> Sr1 = pd.Series([1,2,3,4], index = ['A', 'B', 'C', 'D']) >>> Sr2 = pd.Series([5,6], index = ['A', 'C']) >>> Sr1+Sr2 A     6 B   NaN C     9 D   NaN >>> Sr1.add(Sr2, fill_value=0) A    6 B    2 C    9 D    4 
like image 93
DSM Avatar answered Sep 25 '22 18:09

DSM


Sr1 = pd.Series([1,2,3,4], index = ['A', 'B', 'C', 'D'])  Sr2 = pd.Series([5,6,7], index = ['A', 'C','E']) (Sr1+Sr2).fillna(Sr2).fillna(Sr1) 

An alternative approach using fillna. It will work on all cases when indeces do not match too

like image 26
kartik Garg Avatar answered Sep 23 '22 18:09

kartik Garg