Multiply two pandas DataFrame columns in Python

In this tutorial, you will learn how you can multiply two Pandas DataFrame columns in Python. You will be multiplying two Pandas DataFrame columns resulting in a new column consisting of the product of the initial two columns.

You need to import Pandas first:

import pandas as pd

Now let’s denote the data set that we will be working on as data_set.

data_set = {"col1": [10,20,30], "col2": [40,50,60]}
data_frame = pd.DataFrame(data_set)

You can try to print the data frame and it will show you two columns as:

print(data_frame)

Output:

   col1  col2
0   10    40
1   20    50
2   30    60

Now you can just use the “*” operator between column one and column two of the data frame as:

data_frame["col1*col2"] = data_frame["col1"] * data_frame["col2"]
print(data_frame)

Hence the output will be:

   col1  col2  col1*col2
0    10    40        400
1    20    50       1000
2    30    60       1800

Also read:

Leave a Reply

Your email address will not be published.