The Pandas Series function dot() used to compute the dot product between the Series and the columns of other. It returns either scalar, Series or numpy.ndarray that contain the dot product of the Series and other if other is Series.
other : It is a Series, DataFrame or array to compute the dot product with its columns.
1 import pandas as pd
2 import numpy as np
3
4 ser = pd.Series([0, 1, 2, 3])
5 other = pd.Series([-2, 2, -4, 4])
6
7 res = ser.dot(other)
8
9
10 print('The dot product of Series is :')
11 print(res)
In the above example, a Series objects are created by passing an array. A dot() function is called by passing a Series object that multiply Series object element-wise and perform addition. The result is assigned to the variable and printed on console.
1 The dot product of Series is :
2 6
dot() with other as DataFrame
dot() with other as DataFrame
1 import pandas as pd
2 import numpy as np
3
4 ser = pd.Series([0, 1, 2, 3])
5 other = pd.DataFrame([[0, 1], [-2, 3], [4, -5], [6, 7]])
6
7 res = ser.dot(other)
8 print('The dot product of Series is :')
9 print(res)
In the above example, a Series and DataFrame object is created by passing an array. The dot() function is called by passing a DataFrame object. A dot() function is called by passing an DataFrame object which compute the dot product on columns. The result is assigned to the variable that will be printed on console.
1 The dot product of Series is :
2 0 -24
3 1 28
4 dtype: int64
dot() with other as array
dot() with other as array
1 import pandas as pd
2 import numpy as np
3
4 ser = pd.Series([0, 1, 2, 3])
5 other = np.array([[0, -1], [2, -3], [-4, 5], [-6, 7]])
6
7 res = ser.dot(other)
8 print('The dot product of Series is :')
9 print(res)
1 The dot product of Series is :
2 [-24 28]
Related options for your search