Pandas: Calculate Product of Values with prod()

Compute the product of values over requested Series or DataFrame axes using the Pandas prod() (or product()) method. Calculate compound growth factors, configure axis multiplication, and handle missing observations.

Introduction to prod() 🔝

The prod() method (an alias for product()) returns the product of all numerical elements along a specified axis. By default, missing values are excluded (skipna=True).

Create numerical sample DataFrame 🔝

We start with a multi-column numerical DataFrame representing growth multipliers or factors.
import pandas as pd

df = pd.DataFrame({
    "FACTOR_A": [1, 2, 3, 4],
    "FACTOR_B": [5, 5, 2, 2]
})
print(df)
assert df.shape == (4, 2)

Expected output

   FACTOR_A  FACTOR_B
0         1         5
1         2         5
2         3         2
3         4         2

Basic product calculation 🔝

Call prod() on the DataFrame to compute the product of values down each column.
res = df.prod()
print(res)
assert res["FACTOR_A"] == 24 and res["FACTOR_B"] == 100

Expected output

FACTOR_A     24
FACTOR_B    100
dtype: int64

Row-wise product calculation (axis=1) 🔝

Set axis=1 to compute products across columns for each row.
row_prod = df.prod(axis=1)
print(row_prod)
assert row_prod.iloc[0] == 5 and row_prod.iloc[1] == 10

Expected output

0     5
1    10
2     6
3     8
dtype: int64

Exercise: compute cumulative growth product 🔝

Given a Series factors = pd.Series([1.1, 1.2, 1.05], name="GROWTH"), compute its product using prod() and verify that it rounds to 1.386.

Exercise solution 🔝

Solution creating the Series and calculating prod().
factors = pd.Series([1.1, 1.2, 1.05], name="GROWTH")
answer = factors.prod()
print(f"Answer: {answer:.3f}")
assert round(answer, 3) == 1.386

Expected output

Answer: 1.386

Practice in Google Colab 🔝

Interactive Colab Notebook

Practice prod() examples interactively directly in your browser:

Open In Colab View on GitHub

Reference: Pandas prod() documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer