How do I multiply two columns in Pandas with examples?

How do I multiply two columns in Pandas with examples?

Pandas is a popular library in Python used for data analysis and manipulation. It offers a convenient way to perform calculations on data, including multiplying two columns. To multiply two columns in Pandas, you can use the “multiply()” function, which multiplies elements of two Series or DataFrames. For example, if you have a DataFrame with columns representing sales quantity and price, you can use the “multiply()” function to calculate the total revenue by multiplying these two columns. The resulting column will contain the product of the corresponding elements from the two columns. This functionality is useful for various data analysis tasks, such as computing total sales, profits, or expenses. With Pandas, performing calculations on data is made easier and more efficient.

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
  • The product of price and amount if type is equal to “Sale”
  • 0 otherwise

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

Cite this article

stats writer (2024). How do I multiply two columns in Pandas with examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-multiply-two-columns-in-pandas-with-examples/

stats writer. "How do I multiply two columns in Pandas with examples?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-multiply-two-columns-in-pandas-with-examples/.

stats writer. "How do I multiply two columns in Pandas with examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-multiply-two-columns-in-pandas-with-examples/.

stats writer (2024) 'How do I multiply two columns in Pandas with examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-multiply-two-columns-in-pandas-with-examples/.

[1] stats writer, "How do I multiply two columns in Pandas with examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do I multiply two columns in Pandas with examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top