CBSE CS and IP

CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Mathematical operations on Pandas Series

Mathematical operations on Pandas Series



  1. You can perform arithmetic operations like addition, subtraction, division, multiplication on two Series objects.
  2. The operations are performed only on the matching indexes.
  3. For all non-matching indexes, NaN (Not a Number) will be returned

Note:-  Like NumPy array, series support vector operations. Batch operations on data without writing any for loops. This is usually called vectorization.

Let us consider the following two Series S1 and S2. We will perform mathematical operations on these Series.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import pandas as pd
s1 = pd.Series(data = [ 1 , 2 , 3 , 4 , 5 ], index = [ 'A' , 'B' , 'C' , 'D' , 'E' ])
s2 = pd.Series(data = [ 1 , 2 , 3 , 4 , 5 ], index = [ 'B' , 'C' , 'D' , 'E' , 'F' ])
print(s1)

A 1
B 2
C 3
D 4
E 5
dtype: int64

print(s2)

B 1
C 2
D 3
E 4
F 5
dtype: int64


Now let us perform mathematical operations on these two Series.
  1. Addition:  We can use the '+' Operator or add() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    >>> s1 + s2                                                 
    A    NaN
    B    3.0
    C    5.0
    D    7.0
    E    9.0
    F    NaN
    dtype: float64
    
    >>> s1.add(s2)
    A    NaN
    B    3.0
    C    5.0
    D    7.0
    E    9.0
    F    NaN
    dtype: float64
     
    

  2. Subtraction: We can use the '-' Operator or sub() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    >>> s1 - s2
    A    NaN
    B    1.0
    C    1.0
    D    1.0
    E    1.0
    F    NaN
    dtype: float64
    
    >>> s1.sub(s2)                                      
    A    NaN
    B    1.0
    C    1.0
    D    1.0
    E    1.0
    F    NaN
    dtype: float64
    

  3. Division: We can use the '/' Operator or div() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    >>> s1 / s2
    A         NaN
    B    2.000000
    C    1.500000
    D    1.333333
    E    1.250000
    F         NaN
    dtype: float64                                         
    
    >>> s1.div(s2)
    A         NaN
    B    2.000000
    C    1.500000
    D    1.333333
    E    1.250000
    F         NaN
    dtype: float64
    

  4. Multiplication: We can use the '*' Operator or mul() method of Series to perform addition between two Series Objects.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    >>> s1*s2
    A     NaN
    B     2.0
    C     6.0
    D    12.0
    E    20.0
    F     NaN
    dtype: float64                                      
     
    >>> s1.mul(s2)
    A     NaN
    B     2.0
    C     6.0
    D    12.0
    E    20.0
    F     NaN
    dtype: float64
    



No comments:

Post a Comment