How to use the Series.product() function in pandas

Overview

The Series.product() function in pandas computes the product of the elements in the series.

Syntax

Series.product(axis=None, skipna=True, level=None, numeric_only=None, min_count=0)

Parameters

  • axis: This indicates the axis in which the operation is to be performed. The value 0 and 1 indicates rows and columns, respectively. The default is 0.
  • skipna: This is used to eliminate NAN or null values when calculating the result. The default is True.
  • level (optional): The default value is None. It represents the level to be broadcasted(in the case of multilevel).
  • numeric_only: Only float, int, and boolean columns are allowed. If there is None, it will try to use everything. Otherwise, it will just use numerical data. Not used during the Series.
  • min_count: This represents the minimum number of valid values needed to complete the operation. The default is 0.

Code

The following code will demonstrate how to use the Series.product() function in pandas:

import pandas as pd
import numpy as np
# create Series
my_series = pd.Series([17, np.nan, 28, 15, 23, 7, np.nan])
# compute the product of the series
# using product()
print(my_series.product(skipna=True))

Explanation

In the code above:

  • Line 5: We create the Series, my_series.

  • Line 9: We use the product() function to return the product of the Series elements.

Note: The NaN values are dropped by setting the skipna argument to True.

Free Resources