How to Multiply Two Columns in Pandas (With Examples)


You can use the following methods to multiply two columns in a pandas DataFrame:

Method 1: Multiply Two Columns

df['new_column'] = df.column1 * df.column2

Method 2: Multiply Two Columns Based on Condition

new_column = df.column1 * df.column2

#update values based on condition
df['new_column'] = new_column.where(df.column2 == 'value1', other=0)

The following examples show how to use each method in practice.

Example 1: Multiply Two Columns

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'price': [22, 20, 25, 30, 4, 8, 12, 10],
                   'amount': [3, 1, 3, 3, 2, 4, 3, 5]})

#view DataFrame
print(df)

   price  amount
0     22       3
1     20       1
2     25       3
3     30       3
4      4       2
5      8       4
6     12       3
7     10       5

We can use the following syntax to multiply the price and amount columns and create a new column called revenue:

#multiply price and amount columns
df['revenue'] = df.price * df.amount

#view updated DataFrame
print(df)

   price  amount  revenue
0     22       3       66
1     20       1       20
2     25       3       75
3     30       3       90
4      4       2        8
5      8       4       32
6     12       3       36
7     10       5       50

Notice that the values in the new revenue column are the product of the values in the price and amount columns.

Example 2: Multiply Two Columns Based on Condition

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'price': [22, 20, 25, 30, 4, 8, 12, 10],
                   'amount': [3, 1, 3, 3, 2, 4, 3, 5],
                   'type': ['Sale', 'Refund', 'Sale', 'Sale',
                            'Sale', 'Refund', 'Refund', 'Sale']})

#view DataFrame
print(df)

   price  amount    type
0     22       3    Sale
1     20       1  Refund
2     25       3    Sale
3     30       3    Sale
4      4       2    Sale
5      8       4  Refund
6     12       3  Refund
7     10       5    Sale

We can multiply together the price and amount columns and then use the where() function to modify the results based on the value in the type column:

#multiply price and amount columns
revenue = df.price * df.amount

#update values based on type
df['revenue'] = revenue.where(df.type == 'Sale', other=0)

#view updated DataFrame
print(df)

   price  amount    type  revenue
0     22       3    Sale       66
1     20       1  Refund        0
2     25       3    Sale       75
3     30       3    Sale       90
4      4       2    Sale        8
5      8       4  Refund        0
6     12       3  Refund        0
7     10       5    Sale       50

Notice that the revenue column takes on the following values:

  • The product of price and amount if type is equal to “Sale”
  • 0 otherwise

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Select Columns by Index in a Pandas DataFrame
How to Rename Index in Pandas DataFrame
How to Drop Columns by Index in Pandas

Leave a Reply

Your email address will not be published.