Pandas DataFrame.dot


Pandas DataFrame.dot

The DataFrame.dot method in pandas performs matrix multiplication between a DataFrame and another DataFrame, Series, or NumPy array. It is useful for linear algebra operations and computing dot products.


Syntax

The syntax for DataFrame.dot is:

DataFrame.dot(other)

Here, DataFrame refers to the pandas DataFrame, and other is the object to multiply with.


Parameters

ParameterDescription
otherThe object to perform the dot product with. Can be a DataFrame, Series, or NumPy array. The dimensions must align appropriately for the dot product.

Returns

A DataFrame or Series resulting from the dot product.


Examples

Dot Product with Another DataFrame

Perform a dot product between two DataFrames.

Python Program

import pandas as pd

# Create two DataFrames
df1 = pd.DataFrame({
    'A': [1, 2],
    'B': [3, 4]
})

df2 = pd.DataFrame({
    'X': [5, 6],
    'Y': [7, 8]
})

# Perform dot product
print("Dot product of two DataFrames:")
result = df1.dot(df2)
print(result)

Output

Dot product of two DataFrames:
    X   Y
0  26  30
1  38  44

Dot Product with a Series

Compute the dot product between a DataFrame and a Series.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Create a Series
s = pd.Series([7, 8], index=['A', 'B'])

# Perform dot product
print("Dot product of DataFrame and Series:")
result = df.dot(s)
print(result)

Output

Dot product of DataFrame and Series:
0    39
1    54
2    69
dtype: int64

Dot Product with a NumPy Array

Compute the dot product between a DataFrame and a NumPy array.

Python Program

import pandas as pd
import numpy as np

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Create a NumPy array
arr = np.array([[7, 8], [9, 10]])

# Perform dot product
print("Dot product of DataFrame and NumPy array:")
result = df.dot(arr)
print(result)

Output

Dot product of DataFrame and NumPy array:
    0   1
0  43  48
1  61  68
2  79  88

Validating Dimensions

Ensure that the dimensions of the DataFrame and the other object align appropriately. Otherwise, a ValueError will be raised.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Create a Series with mismatched dimensions
s = pd.Series([7, 8, 9], index=['A', 'B', 'C'])

# Attempt dot product
print("Attempting dot product with mismatched dimensions:")
try:
    result = df.dot(s)
except ValueError as e:
    print("Error:", e)

Output

Attempting dot product with mismatched dimensions:
Error: matrices are not aligned

Summary

In this tutorial, we explored the DataFrame.dot method in pandas. Key takeaways include:

  • Using dot to perform matrix multiplication with DataFrames, Series, or NumPy arrays.
  • Ensuring dimensional alignment to avoid errors.
  • Applying dot products for linear algebra operations and data analysis.

The DataFrame.dot method is a powerful tool for performing matrix operations in pandas.


Python Libraries