In [ ]: Mathematical Operations on Series
1)All mathematical operation(+,-,*,/)
done based on matching index between 2 series
2)We can also use mathematical functions like
add(),sub(),mul(),div() to such operations.
3)We can also do mathematical operations with scalar value
or one fixed value.
In [2]: #Adding two series with default matching index
import pandas as pd
S1=pd.Series([23,10,15])
S2=pd.Series([3,0,5])
S1+S2
Out[2]: 0 26
1 10
2 20
dtype: int64
In [4]: #Adding two series with user defined matching index
import pandas as pd
S3=pd.Series([23,10,15],index=['a','b','c'])
S4=pd.Series([3,0,5],index=['a','b','c'])
S3+S4
Out[4]: a 26
b 10
c 20
dtype: int64
In [5]: #Adding two series with mismatch user defined index
import pandas as pd
S3=pd.Series([23,10,15],index=['a','b','c'])
S4=pd.Series([3,0,5],index=['d','b','e'])
S3+S4
#note that for mismatch index NaN value came
Out[5]: a NaN
b 10.0
c NaN
d NaN
e NaN
dtype: float64
In [7]: #Adding two series with mismatch user defined index of different datatype
import pandas as pd
S3=pd.Series([23,10,15],index=[7,9.8,'a'])
S4=pd.Series([3,0,5],index=['a',6,'e'])
S3+S4
#note that those whose index is numbers that displayed first
#and then letters are displayed
Out[7]: 6 NaN
7 NaN
9.8 NaN
a 18.0
e NaN
dtype: float64
In [8]: #Adding two series using mathematical functions
import pandas as pd
S3=pd.Series([23,10,15],index=['a','b','c'])
S4=pd.Series([3,0,5],index=['d','b','e'])
S3.add(S4)
Out[8]: a NaN
b 10.0
c NaN
d NaN
e NaN
dtype: float64
In [2]: #Multiplying two series using mathematical functions
import pandas as pd
S3=pd.Series([23,10,15],index=['a',7,'c'])
S4=pd.Series([3,10,5],index=['d',7.0,'e'])
S3.mul(S4)
Out[2]: 7 100.0
a NaN
c NaN
d NaN
e NaN
dtype: float64
In [4]: #Substracting two series using mathematical functions
import pandas as pd
S3=pd.Series([23,10,15],index=['a','b','c'])
S4=pd.Series([3,0,5],index=['d','b','e'])
S4.rsub(S3)
Out[4]: a NaN
b 10.0
c NaN
d NaN
e NaN
dtype: float64
In [13]: #Dividing two series using mathematical functions
import pandas as pd
S3=pd.Series([24,10,15],index=['a','b','c'])
S4=pd.Series([3,0,5],index=['a','b','e'])
S3.div(S4)
Out[13]: a 8.0
b inf
c NaN
e NaN
dtype: float64
In [17]: #Vector operations on Series-Any operation to be performed on a series gets
#performed on every single element of it.
import pandas as pd
S=pd.Series([23,10,15],index=['a','b','c'])
S+6
Out[17]: a 29
b 16
c 21
dtype: int64
In [18]: S/3
Out[18]: a 7.666667
b 3.333333
c 5.000000
dtype: float64
In [19]: S//3
Out[19]: a 7
b 3
c 5
dtype: int64
In [20]: S%3
Out[20]: a 2
b 1
c 0
dtype: int64
In [5]: #Consider the String datatype in case of Series mathematical operation
import pandas as pd
St1=pd.Series(['a','b','c',3],index=[1,2,0,3])
print("Printing St1")
print('-------------')
print(St1)
St2=pd.Series(['e','f','g',4],index=[1,2,0,3])
print("Printing St2")
print('-------------')
print(St2)
print("Printing St1+St2")
print('-------------')
print(St1+St2)
print("Printing St1*3")
print('-------------')
print(St1*3)
Printing St1
-------------
1 a
2 b
0 c
3 3
dtype: object
Printing St2
-------------
1 e
2 f
0 g
3 4
dtype: object
Printing St1+St2
-------------
1 ae
2 bf
0 cg
3 7
dtype: object
Printing St1*3
-------------
1 aaa
2 bbb
0 ccc
3 9
dtype: object
In [ ]: