How to multiply two or more columns in Python DataFrames?

In Data Analysis we often need to execute arithmetic operations on our dataset. In today’s tutorial we would like to show how you can easily multiply two or more columns in a single DataFrame or on multiple ones.

Data Preparation

We will start by defining two DataFrames to run through the tutorials examples using the pd.DataFrame() constructor.

import pandas as pd

#define DataFrames
df1 = pd.DataFrame({'employee': [ 'Larry', 'Liam', 'Niall'],
                      'hours_worked': [90,80,80],
                       'salary_hour' : [24.3,31.5,45.4]})

df2= pd.DataFrame({'employee': [ 'Larry', 'Liam', 'Niall'],
                      'hourly_sold_units': [120,90,100]})

Multiply DataFrame columns (by row) element wise

In this example, we’ll create a new DataFrame column by multiplying the values of two additional columns. Here’s the code:

df1['total_salary'] = df1['hours_worked'] * df1['salary_hour']


And here is our DataFrame:

Multiply based on condition

What if we would choose to perform arithmetic operations only on rows values whicht answer a specific boolean condition?

In this example we would like to calculate the salary only for employees who worked over 80 hours.

df1['overtime_salary'] = (df1['hours_worked'] >80)* df1['hours_worked'] * df1['salary_hour']


Multiply columns from different DataFrames

If you have your data in different DataFrames you can obviously concatenate or join then together. You can also create new columns in your Python DataFrame by performing arithmetic operations between matching rows element wise.

df1['total_sales'] = df1['hours_worked'] * df2['hourly_sold_units']


Multiply and sum calculated column

We can easily now go ahead and sum the values of the new calculated column:


Multiply DataFrame columns by specific constant/ value / scalar

If we just want to multiply column values by a constant value:

df1['net_sales'] = df1['total_sales'] * 0.77

Next suggested Learning