How to multiply two or multiple columns in a pandas DataFrame

Renesh Bedre    3 minute read

In this article, you will learn how to multiply multiple columns in a pandas Dataframe

Let’s first create random Pandas DataFrame (you can also import pandas DataFrame from file)

import pandas as pd

# Create DataFrame
df = pd.DataFrame({ 'col1':[10, 11, 13, 25, 282], 
                    'col2':[2, 3, 4, 5, 8],
                    'col3':[0.1, 0.2, 0.3, 0.4, 0.5],
                    'col4': ['A', 'B', 'B', 'B', 'A']})
df
#output
   col1  col2  col3 col4
0    10     2   0.1    A
1    11     3   0.2    B
2    13     4   0.3    B
3    25     5   0.4    B
4   282     8   0.5    A

Multiply two columns

Multiply two columns (col1 and col2) in a df Dataframe,

# multiply and create a new column in df
df['col5'] = df['col1'] * df['col2'] # same as df['col5'] = df.col1 * df.col2 
df
# output
df
   col1  col2  col3 col4  col5
0    10     2   0.1    A    20
1    11     3   0.2    B    33
2    13     4   0.3    B    52
3    25     5   0.4    B   125
4   282     8   0.5    A  2256

The col5 contains the multiplication values

Multiply multiple columns

As similar to two columns, you can also multiply multiple columns in a pandas Dataframe. Here, you will use the Dataframe multiply function, which works like you are multiplying two different Dataframes

# multiply col1, col2, and col3
df['col5'] = pd.DataFrame(df['col1'] * df['col2']).multiply(df['col3'], axis=0)
df
#output
df
   col1  col2  col3 col4    col5
0    10     2   0.1    A     2.0
1    11     3   0.2    B     6.6
2    13     4   0.3    B    15.6
3    25     5   0.4    B    50.0
4   282     8   0.5    A  1128.0

The col5 contains the multiplication values of col1, col2, and col3

Multiply multiple columns by a single column

You can also multiply multiple columns in a pandas Dataframe by values in another single column,

# multiply col1 and col2, by values in col3
df[['col1', 'col2']].multiply(df['col3'], axis=0)
# output
    col1  col2
0    1.0   0.2
1    2.2   0.6
2    3.9   1.2
3   10.0   2.0
4  141.0   4.0

Multiply single or multiple columns by a specific value

You can multiply single or multiple Dataframe columns by any specific value,

Multiply a single column by a specific value,

# multiply col1 by 5
df['col5'] = df['col1'] * 5 # same as df['col5'] = df.col1 * 5
df
#output
df
   col1  col2  col3 col4  col5
0    10     2   0.1    A    50
1    11     3   0.2    B    55
2    13     4   0.3    B    65
3    25     5   0.4    B   125
4   282     8   0.5    A  1410

Multiply two columns by a specific value,

# multiply col1 and col2 by 5
df[['col1', 'col2']] * 5
#output
   col1  col2
0    50    10
1    55    15
2    65    20
3   125    25
4  1410    40

Multiply columns based on a condition

You can also multiply the two columns of a pandas DataFrame based on a conditional value in another column

For example, multiply col1 and col3 where the col4 has B value,

df['col5'] = df.query('col4=="B"').col1 * df.query('col4=="B"').col3
df
# output
   col1  col2  col3 col4  col5
0    10     2   0.1    A   NaN
1    11     3   0.2    B   2.2
2    13     4   0.3    B   3.9
3    25     5   0.4    B  10.0
4   282     8   0.5    A   NaN

Learn more about how to query pandas DataFrame for conditional selection of rows

Enhance your skills with courses on Python and pandas

This work is licensed under a Creative Commons Attribution 4.0 International License

Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.

Tags:

Updated: