Python Pandas - Arithmetic Operations on Series Object



Pandas Series is one of the primary data structures, that stores the one-dimensional labeled data. The data can be any type, such as integers, floats, or strings. One of the primary advantages of using a Pandas Series is the ability to perform arithmetic operations in a vectorized manner. This means arithmetic operations on Series are performed without needing a loop through elements manually.

In this tutorial, we will learn how to apply arithmetic operations like addition(+), subtraction(-), multiplication(*), and division(/) to a single Series or between two Series objects.

Arithmetic Operations on a Series with Scalar Value

Arithmetic operations on a Pandas Series object can be directly applied to an entire Series elements, which means the operation is executed element-wise across all values. This is very similar to how operations work with NumPy arrays.

Following is the list of commonly used arithmetic operations on Pandas Series −

Operation Example Description
Addition s + 2 Adds 2 to each element
Subtraction s - 2 Subtracts 2 from each element
Multiplication s * 2 Multiplies each element by 2
Division s / 2 Divides each element by 2
Exponentiation s ** 2 Raises each element to the power of 2
Modulus s % 2 Finds remainder when divided by 2
Floor Division s // 2 Divides and floors the quotient

Example

The following example demonstrates how to applies the all arithmetical operations on a Series object with the scalar values.

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

# Display the Input Series
print('Input Series\n',s)

# Apply all Arithmetic Operation and Display the Results
print('\nAddition:\n',s+2)
print('\nSubtraction:\n', s-2)
print('\nMultiplication:\n', s * 2)
print('\nDivision:\n', s/2)
print('\nExponentiation:\n', s**2)
print('\nModulus:\n', s%2)
print('\nFloor Division:\n', s//2)

Following is the output of the above code −

Input Series
 a    1
b    2
c    3
d    4
e    5
dtype: int64

Addition:
 a    3
b    4
c    5
d    6
e    7
dtype: int64

Subtraction:
 a   -1
b    0
c    1
d    2
e    3
dtype: int64

Multiplication:
 a     2
b     4
c     6
d     8
e    10
dtype: int64

Division:
 a    0.5
b    1.0
c    1.5
d    2.0
e    2.5
dtype: float64

Exponentiation:
 a     1
b     4
c     9
d    16
e    25
dtype: int64

Modulus:
 a    1
b    0
c    1
d    0
e    1
dtype: int64

Floor Division:
 a    0
b    1
c    1
d    2
e    2
dtype: int64

Arithmetic Operations Between Two Series

You can perform arithmetical operations between two series objects. Pandas automatically aligns the data by index labels. If one of the Series object does not have an index but not the other, then the resultant value for that index will be NaN.

Example

This example demonstrates applying the arithmetic operations on two series objects.

import pandas as pd
s1 = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
s2 = pd.Series([9, 8, 6, 5], index=['x','a','b','c'])

# Apply all Arithmetic Operations and Display the Results
print('\nAddition:\n',s1+s2)
print('\nSubtraction:\n', s1-s2)
print('\nMultiplication:\n', s1 * s2)
print('\nDivision:\n', s1/s2)

Following is the output of the above code −

Addition:
 a    9.0
b    8.0
c    8.0
d    NaN
e    NaN
x    NaN
dtype: float64

Subtraction:
 a   -7.0
b   -4.0
c   -2.0
d    NaN
e    NaN
x    NaN
dtype: float64

Multiplication:
 a     8.0
b    12.0
c    15.0
d     NaN
e     NaN
x     NaN
dtype: float64

Division:
 a    0.125000
b    0.333333
c    0.600000
d         NaN
e         NaN
x         NaN
dtype: float64
Advertisements