How to add two pandas Series objects by handling None values?


In pandas Series functionalities we have a function called add() which is used to add a series object with another series object. It is also used to add a Series object with an integer value and with a python list.

The series.add() method has a fill_values parameter. Which is used to handle the missing values effectively by substituting a float value to this parameter. By default the input to this fill_value parameter is Nan.

Example

import pandas as pd
import numpy as np

sr1 = pd.Series(np.arange(1,6))
print('Series Object 1:',sr1, sep='
') sr2 = pd.Series(np.random.randint(10,20,4)) print('Series Object 2:',sr2, sep='
') result = sr1.add(sr2) print('Resultant series object with the addition of two Series:', result)

Explanation

We have two series objects sr1 and sr2, which are created by using NumPy arange and random functions respectively. The sr1 object has 5 elements and sr2 has only 4 elements.

These two series objects have different lengths. And we are adding these two objects by using the series.add() function.

Output

Series Object 1:
0   1
1   2
2   3
3   4
4   5
dtype: int32
Series Object 2:
0  15
1  12
2  16
3  12
dtype: int32
Resultant series object with addition of two Series: 0 16.0
1   14.0
2   19.0
3   16.0
4    NaN
dtype: float64

Explanation

The output of the series.add() function can be seen in the last lines of the above block. And we can see a NaN value in the resultant output, which happens because both the series are not having the same lengths.

Example

result = sr1.add(sr2, fill_value=0)

print('The resultant series object of adding two series with fill_value:', result)

Explanation

To remove the NaN value in the previous output, here we have substituted the ‘0’ value to the fill_value parameter.

Output

The resultant series object of adding two series with fill_value:
0  16.0
1  14.0
2  19.0
3  16.0
4   5.0
dtype: float64

We can see that there is no NaN value present in this output, which happened because of this fill_value parameter. In this example, we gave 0 as input to this fill_value parameter so that it will add the missing values with 0.

Example

import pandas as pd
import numpy as np

sr1 = pd.Series({'a':1,'b':2,'c':3})
print('Series Object 1:',sr1)

sr2 = pd.Series({'c':7,'d':8,'e':9})

print('Series Object 2:',sr2)

result1 = sr1.add(sr2)

print('Resultant series object without Fill_value:', result1)

result2 = sr1.add(sr2, fill_value= 0)

print('Resultant series object with Fill_value 0:', result2)

Explanation

In this following example, we have created two pandas series objects by using the python dictionaries with different index labels.

And we have done the addition operation between those two series objects in both ways, by substituting the fill_value parameter and without defying the parameter input.

Output

Series Object 1:
a   1
b   2
c   3
dtype: int64

Series Object 2:
c   7
d   8
e   9
dtype: int64

Resultant series object without Fill_value:
a   NaN
b   NaN
c  10.0
d   NaN
e   NaN
dtype: float64

Resultant series object with Fill_value 0:
a   1.0
b   2.0
c  10.0
d   8.0
e   9.0
dtype: float64

The addition between two series’s are done based on the index labels, if the indexes are not same then the add function will automatically match those missing indexes by substituting NaN and then performs the addition operation. That is the reason we can see the NaN values in the resultant output of add() function.

Updated on: 18-Nov-2021

471 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements