Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
python

What is the dot method in pandas?

Hassaan Waqar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The dot method in pandas takes the dot product of a dataframe with another dataframe, series, or array-like object. The dot product is also known as matrix multiplication.

The shapes of both objects should be compatible with each other to calculate the dot product.

The illustration below shows how the dot product is computed:

How to calculate the dot product

Syntax

The syntax of the dot method is as follows:

Dataframe.dot(other)

Parameters

The dot method has a single parameter: other.

Other refers to a dataframe, series, or array-like object with which the dot product is taken.

Return value

The dot method returns a series if the other parameter is a series object.

It returns a dataframe if the other parameter is a dataframe.

Examples

The code snippet below shows how we can use the dot operator in pandas:

import pandas as pd
import numpy as np

# Dataframe with a series
df = pd.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]])
s = pd.Series([1, 1, 2, 1])
print("Dataframe with a series")
print(df.dot(s))
print('\n')

# Dataframe with another Dataframe
other = pd.DataFrame([[0, 1], [1, 2], [-1, -1], [2, 0]])
print("Dataframe with another Dataframe")
print(df.dot(other))
print('\n')

# Dataframe with an array
arr = np.array([[0, 1], [1, 2], [-1, -1], [2, 0]])
print("Dataframe with another array")
print(df.dot(arr))
print('\n')

The dot method is the same as the @ operator in pandas.

RELATED TAGS

pandas
python

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring