Python | Pandas Dataframe/Series.dot()
Pandas Series.dot()
The dot()
method is used to compute the dot product between DataFrames
or Series
.
Pandas Series.dot() is used to compute the dot product between DataFrames
or Series
. It works similarly to pd.mul() method, but instead of returning multiplied separate values, the dot product is returned (the sum of the multiplication of values at each index).
Syntax:
Syntax: Series.dot(other)
Parameters:
- other: Other Series to be used to calculate DOT product
Return of the Series.dot() function
The return of the dot function can be a scalar, series, or numpy.ndarray.
- The dot product of the Series and other if other is a Series.
- The Series of the dot product of Series and each rows of other if other is a DataFrame .
- A numpy.ndarray between the Series and each columns of the numpy array.
pandas Series.dot() Method Implementations
Importing necessary libraries
Python3
# importing pandas module import pandas as pd # importing numpy module import numpy as np |
1. With Series Data
In this example, two series are created from Python lists using Pandas Series() method. The method is then called on series1 and series2 is passed as a parameter. The result is then stored in a variable and displayed.
Python3
# creating series 1 series1 = pd.Series([ 7 , 5 , 6 , 4 , 9 ]) # creating series 2 series2 = pd.Series([ 1 , 2 , 3 , 10 , 2 ]) # storing in new variable # calling .dot() method ans = series1.dot(series2) # display print ( 'Dot product = {}' . format (ans)) |
Output:
Dot product = 93
Explanation:
The elements in the caller series are multiplied by the element at the same index in the past series. All the multiplied values are then added to get the dot product. As in the above example, the series are:
[7, 5, 6, 4, 9] [1, 2, 3, 10, 2]
Dot product = 7*1 + 5*2 + 6*3 + 4*10 + 9*2 = 7 + 10 + 18 + 40 + 18 = 93
2. With Dataframe
Python3
series = pd.Series([ 5 , 3 , 7 , 4 ], name = 'Series' ) # Creating a DataFrame df1 df1 = pd.DataFrame([[ 0 , 1 , - 9 , 1 ], [ 9 , 1 , 0 , 1 ], [ 1 , 3 , 1 , - 1 ], [ 1 , 1 , 8 , 1 ]]) # Calculating dot product with each row in the DataFrame result_series = series.dot(df1) # Printing the resulting Series print (result_series) |
Output:
0 38
1 33
2 -6
3 5
Name: Series, dtype: int64
Explanation:
The resulting Series will have values obtained by taking the dot product of the Series [5, 3, 7, 4]
with each column in the DataFrame df1.
- For the first row of
df1
: 0β 5+1β 3+(β9)β 7+1β 4=38 - For the second row of
df1
: 9β 5+1β 3+0β 7+1β 4=33 - For the third row of
df1
: 1β 5+3β 3+1β 7+(β1)β 4=β6 - For the fourth row of
df1
: 1β 5+1β 3+8β 7+1β 4=5
Note: You do not need to take the transpose in this case. The dot product operation between a Series and a DataFrame in pandas is designed to handle this situation without the need for transposing.
3. With Numpy Array
Python3
# Given NumPy array or array-like numpy_array = np.array([ 1 , 2 , 3 , 4 ]) # Creating a pandas Series series = pd.Series([ 5 , 3 , 7 , 4 ], name = 'Series' ) # Performing element-wise multiplication result_series = series * numpy_array # Printing the resulting Series print (result_series) |
Output:
0 5
1 6
2 21
3 16
Name: Series, dtype: int64
Output is
a pandas Series where each element is the product of the corresponding elements in the original Series and NumPy array, i.e [5 * 1, 3 * 2, 7 * 3, 4 * 4]
, resulting in [5, 6, 21, 16]
.
Pandas Dataframe.dot()Β Method Implementations
We can also use Pandas Dataframe for using the dot() method. The dot() method multiplies each value from one DataFrame with the values from another DataFrame, and adds them together.
Syntax:
Syntax: dataframe.dot(other)
Parameters:
- other: Other Dataframe to be used to calculate DOT product
Return of Dataframe.dot()
- If
other
is a Series: It returns a Series representing the dot product of the DataFrame and the Series. - If
other
is a DataFrame: It returns a Series representing the dot product of the DataFrame and each row of the other DataFrame. - If
other
is a NumPy array or array-like: It returns a Series representing the dot product between the DataFrame and each column of the NumPy array.
pandas DataFrame.dot() Method Implementations
1. With Series Data
All the multiplied values are then added to get the dot product.
Python3
# Create a DataFrame df = pd.DataFrame({ 'A' : [ 1 , 2 , 3 ], 'B' : [ 4 , 5 , 6 ], 'C' : [ 7 , 8 , 9 ] }) # Create a Series series = pd.Series([ 0.1 , 0.2 , 0.3 ], index = [ 'A' , 'B' , 'C' ]) # Dot product with a Series result_series = df.dot(series) print ( "Dot product with Series:" ) print (result_series) |
Output:
Dot product with Series:
0 3.0
1 3.6
2 4.2
dtype: float64
Explanation:
- Dot product for row 0: 1Γ0.1+4Γ0.2+7Γ0.3=3.01Γ0.1+4Γ0.2+7Γ0.3=3.0
- Dot product for row 1: 2Γ0.1+5Γ0.2+8Γ0.3=3.62Γ0.1+5Γ0.2+8Γ0.3=3.6
- Dot product for row 2: 3Γ0.1+6Γ0.2+9Γ0.3=4.23Γ0.1+6Γ0.2+9Γ0.3=4.2
2. With Dataframe
Python3
# Creating a DataFrame df1 df1 = pd.DataFrame([[ 0 , 1 , - 9 , 1 ], [ 9 , 1 , 0 , 1 ], [ 1 , 3 , 1 , - 1 ], [ 1 , 1 , 8 , 1 ]]) # Creating another DataFrame df2 df2 = pd.DataFrame([[ 5 , 3 , 7 , 4 ], [ 1 , 3 , 4 , 3 ], [ 4 , 3 , 8 , 6 ], [ 5 , 8 , 2 , 8 ]]) # Calculating dot product result = df1.dot(df2) # Printing the result print (result) |
Output:
0 1 2 3
0 -30 -16 -66 -43
1 51 38 69 47
2 7 7 25 11
3 43 38 77 63
Explanation:
- Each row in the resulting DataFrame represents the dot product of the corresponding row in
df1
and the columns indf2
. - For example, the first row (row 0) is calculated as follows:
- 0Γ5+1Γ3+(β9)Γ7+1Γ4=β300Γ5+1Γ3+(β9)Γ7+1Γ4=β30
- 0Γ1+1Γ3+(β9)Γ3+1Γ8=β160Γ1+1Γ3+(β9)Γ3+1Γ8=β16
- 0Γ4+1Γ3+(β9)Γ8+1Γ2=β660Γ4+1Γ3+(β9)Γ8+1Γ2=β66
- 0Γ5+1Γ8+(β9)Γ2+1Γ8=β430Γ5+1Γ8+(β9)Γ2+1Γ8=β43
- The same logic is applied to calculate each element in the resulting DataFrame.
3. With Numpy Array
Python3
# Creating a DataFrame df1 df1 = pd.DataFrame([[ 0 , 1 , - 9 , 1 ], [ 9 , 1 , 0 , 1 ], [ 1 , 3 , 1 , - 1 ], [ 1 , 1 , 8 , 1 ]]) # Creating a NumPy array numpy_array = np.array([ 5 , 3 , 7 , 4 ]) # Calculating dot product with NumPy array result = df1.dot(numpy_array) # Printing the result print (result) |
Output:
0 -56
1 52
2 17
3 68
dtype: int64
Explanation:
Each row represents the dot product of the corresponding row in the original DataFrame df1
with the NumPy array numpy_array.
- For the first row: 0 * 5 + 1 * 3 + (-9) * 7 + 1 * 4 = -56
- For the second row: 9 * 5 + 1 * 3 + 0 * 7 + 1 * 4 = 52
- For the third row: 1 * 5 + 3 * 3 + 1 * 7 + (-1) * 4 = 17
- For the fourth row: 1 * 5 + 1 * 3 + 8 * 7 + 1 * 4 = 68
Contact Us