Finding dot product of two pandas Series objects

The dot product of two sequences is found by multiplying the corresponding elements and summing the multiplication results. The Python example code uses Series.dot() to find the dot product of sequences represented by two pandas Series objects.

Series Binaryoperatorfunctions Updated 26 Aug 2019 1 min read

Overview

  • In mathematics, a dot product of two sequences is given by:

x.y = Sum(x1.y1 + x2.y2+…+xnyn)

  • A pandas Series is a one-dimensional sequence built using numpy.ndarray and bundled with numerous methods to perform computing and data analysis.
  • The dot product of two pandas series objects can be computed using the Series.dot() method.

Example

python
# Example program to perform dot product

# between two pandas series objects

import pandas as pds

# Create pandas.Series objects

series1 = pds.Series([1, 0, 5]);

series2 = pds.Series([2, 1, 2]);

# Obtain dot product of two pandas Series objects

series3 = series1.dot(series2);

print("Series1:");

print(series1);

print("Series2:");

print(series2);

print("Dot product of Series1 and Series2");

print(series3);

Output

Output
Series1:

0    1

1    0

2    5

dtype: int64

Series2:

0    2

1    1

2    2

dtype: int64

Dot product of Series1 and Series2

12