Dark Mode On/Off

# Pandas DataFrame dot() Method

In this tutorial, we will learn the python pandas `DataFrame.dot()` method. It computes the matrix multiplication between the DataFrame and others. This method computes the matrix product between the DataFrame and the values of another `Series`, `DataFrame` or a` numpy array`. It returns a Series or DataFrame.

• The dimensions of DataFrame and other must be compatible in order to compute the matrix multiplication.

• In addition, the column names of DataFrame and the index of other must contain the same values, as they will be aligned prior to the multiplication.

The below shows the syntax of the `DataFrame.dot()` method.

### Syntax

``DataFrame.dot(other)``

### Parameters

Other: Series, DataFrame or array-like. The other object to compute the matrix product with.

## Example 1: The DataFrame.dot() Method in Pandas

In the below example, two DataFrames are created and the elements in one DataFrame are multiplied with the elements in the other DataFrame. The `DataFrame.dot()` method returns the DataFrame by adding all multiplied values.

``````#importing pandas as pd
import pandas as pd
#creating DataFrames
df1=pd.DataFrame([[0,1,1, 2],[2,1,1,0]],columns=('A','B','C','D'))
df2=pd.DataFrame([[1, 2], [2, 3],[2, 3],[4,1]],index=('A','B','C','D'))
print(df1)
print(df2)
print(df1.dot(df2))``````

Once we run the program we will get the following output.

A B C D
0 0 1 1 2
1 2 1 1 0
0 1
A 1 2
B 2 3
C 2 3
D 4 1
0 1
0 12 8
1 6 10

## Example 2: The DataFrame.dot() Method in Pandas

The below example is similar to the previous one. Create two DataFrames, apply `DataFrame.dot()` method and get the matrix multiplication DataFrame.

``````#importing pandas as pd
import pandas as pd
#creating DataFrames
df1= pd.DataFrame([[1, 1, 1],[2, 2, 2],[3, 3, 3]])
df2= pd.DataFrame([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
print(df1.dot(df2))``````

Once we run the program we will get the following output.

0 1 2
0 1 1 1
1 2 2 2
2 3 3 3

## Example 3: Computing matrix multiplication using the DataFrame.dot() method with `Series`.

In the below example, one DataFrame and Series is created and the elements in one DataFrame are multiplied with the elements in the Series. The `DataFrame.dot()` method returns the DataFrame by adding all multiplied values.

``````#importing pandas as pd
import pandas as pd
#creating DataFrames
df1= pd.DataFrame([[1, 1, 1],[2, 2, 2],[3, 3, 3]],columns=('a','b','c'))
df2=pd.Series([1, 1, 2],index=('a','b','c'))
print(df1)
print(df2)
print(df1.dot(df2))
``````

Once we run the program we will get the following output.

a b c
0 1 1 1
1 2 2 2
2 3 3 3
a 1
b 1
c 2
dtype: int64
0 4
1 8
2 12
dtype: int64

## Example 4: ValueError While multiplying DataFrame

We will get the `ValueError` exception if the matrices are not aligned. The below example shows the same.

``````#importing pandas as pd
import pandas as pd
#creating DataFrames
df1=pd.DataFrame([[0, 1], [1, 2],[2, 0]])
df2=pd.DataFrame([[1, 2], [2, 3],[2, 3]])
print(df1.dot(df2))``````

Once we run the program we will get the following output.

ValueError: matrices are not aligned

### Conclusion

In this tutorial, we learned the python pandas `DataFrame.dot()` method. We solved the example by multiplying the DataFrame with `DataFrame`, `Series`, `np.array` and got the matrix multiplication of DataFrame.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6719936,"math_prob":0.98478043,"size":3205,"snap":"2023-40-2023-50","text_gpt3_token_len":931,"char_repetition_ratio":0.18119338,"word_repetition_ratio":0.26719058,"special_character_ratio":0.30390015,"punctuation_ratio":0.17906336,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.999356,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T01:32:45Z\",\"WARC-Record-ID\":\"<urn:uuid:526a0aab-6321-4a12-91e1-fccb417853d0>\",\"Content-Length\":\"274370\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7ab65247-11a7-4dec-85d0-7877f0651cf2>\",\"WARC-Concurrent-To\":\"<urn:uuid:a6884d16-04c0-478c-a5ea-7726dcb1a578>\",\"WARC-IP-Address\":\"65.2.112.116\",\"WARC-Target-URI\":\"https://www.studytonight.com/pandas/pandas-dataframe-dot-method\",\"WARC-Payload-Digest\":\"sha1:J3HNCCYAVFHOAG47KZYPYIX7GGTKNJHD\",\"WARC-Block-Digest\":\"sha1:KHJBVA3HVXK4NKCMUHOLEAO7TMDTQLO7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506320.28_warc_CC-MAIN-20230922002008-20230922032008-00054.warc.gz\"}"}