We have learnt basic mathematical operations like addition, subtraction, multiplication, division, etc., on two NumPy arrays, the operation is done on each corresponding pair of elements. Similarly, we can perform mathematical operations on two series in Pandas.
While performing mathematical operations on series, index matching is implemented and all missing values are filled in with NaN by default.
Consider the following series: seriesA and seriesB for understanding mathematical operations on series in Pandas.
>>> seriesA = pd.Series([1,2,3,4,5], index = ['a', 'b', 'c', 'd', 'e'])
>>> seriesA
a 1
b 2
c 3
d 4
e 5
dtype: int64
>>> seriesB = pd.Series([10,20,-10,-50,100],
index = ['z', 'y', 'a', 'c', 'e'])
>>> seriesB
z 10
y 20
a -10
c -50
e 100
dtype: int64
(A) Addition of two Series
It can be done in two ways. In the first method, two series are simply added together, as shown in the following code. Table shows the detailed values that were matched while performing the addition. Note here that the output of addition is NaN if one of the elements or both elements have no value.
>>> seriesA + seriesB
a -9.0
b NaN
c -47.0
d NaN
e 105.0
y NaN
z NaN
dtype: float64
Details of addition of two series

The second method is applied when we do not want to have NaN values in the output. We can use the series method add() and a parameter fill_value to replace missing value with a specified value. That is, calling seriesA.add(seriesB) is equivalent to calling seriesA+seriesB, but add() allows explicit specification of the fill value for any element in seriesA or seriesB that might be missing, as shown in Table.
>>> seriesA.add(seriesB, fill_value=0)
a -9.0
b 2.0
c -47.0
d 4.0
e 105.0
y 20.0
z 10.0
dtype: float64
Details of addition of two series using add() method

Note that Table shows the changes in the series elements and corresponding output without replacing the missing values, while Table shows the changes in the series elements and corresponding output after replacing missing values by 0. Just like addition, subtraction, multiplication and division can also be done using corresponding mathematical operators or explicitly calling of the appropriate method.
(B) Subtraction of two Series
Again, it can be done in two different ways, as shown in the following examples:
>>> seriesA – seriesB #using subtraction operator
a 11.0
b NaN
c 53.0
d NaN
e -95.0
y NaN
z NaN
dtype: float64
Let us now replace the missing values with 1000 before subtracting seriesB from seriesA using explicit subtraction method sub().
>>> seriesA.sub(seriesB, fill_value=1000)
# using fill value 1000 while making explicit
# call of the method”
a 11.0
b -998.0
c 53.0
d -996.0
e -95.0
y 980.0
z 990.0
dtype: float64
(C) Multiplication of two Series
Again, it can be done in two different ways, as shown in the following examples:
>>>seriesA * seriesB #using multiplication operator
a -10.0
b NaN
c -150.0
d NaN
e 500.0
y NaN
z NaN
dtype: float64
Let us now replace the missing values with 0 before multiplication of seriesB with seriesA using explicit multiplication method mul().
>>> seriesA.mul(seriesB, fill_value=0)
# using fill value 0 while making
#explicit call of the method
a -10.0
b 0.0
c -150.0
d 0.0
e 500.0
y 0.0
z 0.0
dtype: float64
(D) Division of two Series
Again, it can be done in two different ways, as shown in the following examples:
>>> seriesA/seriesB # using division operator
a -0.10
b NaN
c -0.06
d NaN
e 0.05
y NaN
z NaN
dtype: float64
Let us now replace the missing values with 0 before dividing seriesA by seriesB using explicit division method div().
# using fill value 0 while making explicit # call of the method
a -0.10
b inf c -0.06
d inf
e 0.05
y 0.00
z 0.00
dtype: float64