How to Multiply two or more Columns in Pandas [5 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
6 min

banner

# Table of Contents

  1. How to Multiply two or more Columns in Pandas
  2. Multiply two Columns in Pandas based on a condition
  3. Multiply two Columns in Pandas based on a condition using apply()
  4. Multiply two columns in a DataFrame using mul()

# How to Multiply two or more Columns in Pandas

To multiply two or more columns in a Pandas DataFrame:

  1. Select the columns using bracket notation.
  2. Use the multiplication * operator to multiply the columns.
  3. Optionally assign the multiplication result to a new DataFrame column.
main.py
import pandas as pd df = pd.DataFrame({ 'price': [10, 20, 30, 40], 'amount': [2, 4, 5, 6], 'product': ['apple', 'pear', 'apple', 'banana'] }) print(df) df['total'] = df['price'] * df['amount'] print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following result.

shell
price amount product 0 10 2 apple 1 20 4 pear 2 30 5 apple 3 40 6 banana -------------------------------------------------- price amount product total 0 10 2 apple 20 1 20 4 pear 80 2 30 5 apple 150 3 40 6 banana 240

multiply two columns in pandas

The code sample shows how to multiply the price and amount columns of the DataFrame and assign the results to the total column.

The same approach can be used to multiply more than two columns in a DataFrame.

To get the correct multiplication result, make sure the column values are of type int or float.

main.py
import pandas as pd df = pd.DataFrame({ 'price': ['10', '20', '30', '40'], 'amount': [2, 4, 5, 6], 'product': ['apple', 'pear', 'apple', 'banana'] }) print(df) df['total'] = df['price'].astype('int') * df['amount'].astype('int') print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
price amount product 0 10 2 apple 1 20 4 pear 2 30 5 apple 3 40 6 banana -------------------------------------------------- price amount product total 0 10 2 apple 20 1 20 4 pear 80 2 30 5 apple 150 3 40 6 banana 240

convert to int before multiplying

The values in the price column are strings, so we used the astype() method to convert them to integers before multiplying.

# Multiply two Columns in Pandas based on a condition

You can use the DataFrame.where() method if you need to multiply two columns in a DataFrame based on a condition.

main.py
import pandas as pd df = pd.DataFrame({ 'price': [10, 20, 30, 40], 'amount': [2, 4, 5, 6], 'product': ['apple', 'pear', 'apple', 'banana'] }) print(df) total = df['price'] * df['amount'] df['total'] = total.where(df['product'] == 'apple', other=0) print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
price amount product 0 10 2 apple 1 20 4 pear 2 30 5 apple 3 40 6 banana -------------------------------------------------- price amount product total 0 10 2 apple 20 1 20 4 pear 0 2 30 5 apple 150 3 40 6 banana 0

multiply two columns based on condition

If the product column stores a value of apple then the multiplication result is added to the total column, otherwise, 0 is returned.

main.py
df['total'] = total.where(df['product'] == 'apple', other=0)

In other words, entries for which the supplied condition is False are replaced with the values from the other argument.

# Multiply two Columns in Pandas based on a condition using apply()

You can also use the DataFrame.apply() method to multiply two columns in a DataFrame based on a condition.

main.py
import pandas as pd df = pd.DataFrame({ 'price': [10, 20, 30, 40], 'amount': [2, 4, 5, 6], 'product': ['apple', 'pear', 'apple', 'banana'] }) print(df) print('-' * 50) df['total'] = df.apply( lambda row: ( row['price'] * row['amount'] if row['product'] == 'apple' else 0 ), axis=1 ) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
price amount product 0 10 2 apple 1 20 4 pear 2 30 5 apple 3 40 6 banana -------------------------------------------------- price amount product total 0 10 2 apple 20 1 20 4 pear 0 2 30 5 apple 150 3 40 6 banana 0

multiply two columns based on condition using apply

The DataFrame.apply() method applies a function along an axis of the DataFrame.

Setting the axis argument to 1 is very important because we want to pass each row to the lambda function.

main.py
df['total'] = df.apply( lambda row: ( row['price'] * row['amount'] if row['product'] == 'apple' else 0 ), axis=1 )

The axis argument determines the axis along which the function is applied.

By default, the axis argument is set to 0, which means that it is applied to each column.

We set the axis argument to 1 to apply the function to each row.

The function multiplies the values in the price and amount columns if the corresponding product value is equal to "apple", otherwise, 0 is returned.

You don't necessarily have to use an inline lambda function.

main.py
import pandas as pd df = pd.DataFrame({ 'price': [10, 20, 30, 40], 'amount': [2, 4, 5, 6], 'product': ['apple', 'pear', 'apple', 'banana'] }) print(df) print('-' * 50) def get_total(row): if row['product'] == 'apple': return row['price'] * row['amount'] else: return 0 df['total'] = df.apply(get_total, axis=1) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
price amount product 0 10 2 apple 1 20 4 pear 2 30 5 apple 3 40 6 banana -------------------------------------------------- price amount product total 0 10 2 apple 20 1 20 4 pear 0 2 30 5 apple 150 3 40 6 banana 0

The get_total function gets called with each row.

The function returns the multiplication result if the corresponding product is equal to "apple", otherwise, 0 is returned.

# Multiply two columns in a DataFrame using mul()

You can also use the mul() method to multiply two columns in a DataFrame.

main.py
import pandas as pd df = pd.DataFrame({ 'price': [10, 20, 30, 40], 'amount': [2, 4, 5, 6], 'product': ['apple', 'pear', 'apple', 'banana'] }) print(df) print('-' * 50) df['total'] = df['price'].mul(df['amount']) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
price amount product 0 10 2 apple 1 20 4 pear 2 30 5 apple 3 40 6 banana -------------------------------------------------- price amount product total 0 10 2 apple 20 1 20 4 pear 80 2 30 5 apple 150 3 40 6 banana 240

The mul() method is equivalent to df['A'] * df['B'], however, it has a fill_value argument for missing data in either of the inputs.

main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'price': [10, 20, 30, 40], 'amount': [np.nan, 4, 5, np.nan], 'product': ['apple', 'pear', 'apple', 'banana'] }) print(df) print('-' * 50) df['total'] = df['price'].mul(df['amount'], fill_value=0) print(df)

Running the code sample produces the following output.

shell
price amount product 0 10 NaN apple 1 20 4.0 pear 2 30 5.0 apple 3 40 NaN banana -------------------------------------------------- price amount product total 0 10 NaN apple 0.0 1 20 4.0 pear 80.0 2 30 5.0 apple 150.0 3 40 NaN banana 0.0

The fill_value argument fills missing (NaN) values.

main.py
df['total'] = df['price'].mul(df['amount'], fill_value=0)

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.