Product of values in a pandas dataframe

The prod() function in pandas dataframe class finds the product between values in a DataFrame. Product can be found for columns by specifying axis=0(which is the default) and for rows by specifying axis=1.

Dataframe Computations Updated 2 Jun 2019 1 min read

Overview

  • The python library pandas provides essential data structures and methods to perform data analysis. The DataFrame class in pandas represents a 2 dimensional array.
  • The rows and columns of a pandas DataFrame object can be multiplied and their product is returned as a pandas series using the prod() method .
  • By default, the value for the axis parameter is 0, which calculates the product of columns.
  • To have the data values of rows multiplied, use axis=1.

Example

python
import pandas as pd

dataValue = [(10,11,30,44),

             (10,22,30,55),

             (10,33,30,66)]

dataFrame   = pd.DataFrame(data=dataValue);

print("Dataset:");

print(dataFrame);

product     = dataFrame.prod();

print("Product(Row-wise):");

print(product);

product     = dataFrame.prod(axis=1);

print("Product(Column-wise):");

print(product);

Output

Finding product of the values in a pandas DataFrame object