Last updated: Apr 12, 2024
Reading time·6 min
To multiply two or more columns in a Pandas DataFrame
:
*
operator to multiply the columns.DataFrame
column.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)
Running the code sample produces the following result.
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 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
.
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)
Running the code sample produces the following output.
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 values in the price
column are strings, so we used the
astype() method to convert
them to integers before multiplying.
You can use the
DataFrame.where()
method if you need to multiply two columns in a DataFrame
based on a
condition.
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)
Running the code sample produces the following output.
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
If the product
column stores a value of apple
then the multiplication result
is added to the total
column, otherwise, 0
is returned.
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.
apply()
You can also use the
DataFrame.apply()
method to multiply two columns in a DataFrame
based on a condition.
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)
Running the code sample produces the following output.
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 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.
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.
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.
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)
Running the code sample produces the following output.
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.
DataFrame
using mul()
You can also use the
mul()
method to multiply two columns in a DataFrame
.
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)
Running the code sample produces the following output.
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.
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.
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.
df['total'] = df['price'].mul(df['amount'], fill_value=0)
You can learn more about the related topics by checking out the following tutorials: