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.
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).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
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
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
factors = pd.Series([1.1, 1.2, 1.05], name="GROWTH"), compute its product using prod() and verify that it rounds to 1.386.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 prod() examples interactively directly in your browser:
Reference: Pandas prod() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.