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

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)

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']

df1.head()

And here is our DataFrame:

Multiply based on condition

What if we would choose to perform arithmetic operations only on rows values that answer a specific 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']

df1.head()

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 DF by performing arithmetic operations between matching rows.

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

df1.head()

Sum the calculated column

We can easily sum the values of the new calculated column:

df1['total_sales'].sum()

Multiply column by scalar

If we just want to multiply column values by a scalar:

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

Next Learning