Pandas DataFrame prod() and product() methods

Spread the love

The prod() and product() method in pandas multiplies all values in each column and returns the product for each column. To calculate the product of each rows, we need to set the axis parameter to axis=1 or columns. Both the prod() and product() method works similarly.

Examples –

Let’s create a dataframe to work with.

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
                  'B': [6, 7, 8, 9, 10]})
df

1 . Calculate product of all the values in each columns –

To calculate the product of all the values in each columns we can either use the product() method or the prod() method.

df.product()
#output
A      120
B    30240
dtype: int64

Using prod() method

df.prod()
#output
A      120
B    30240
dtype: int64

2. Calculate product of all the values in each rows –

To calculate the product of all the values in each columns, we need to set the axis parameter to axis=1 or columns.

df.product(axis=1)
#output
0     6
1    14
2    24
3    36
4    50
dtype: int64

Rating: 1 out of 5.

Leave a Reply