Pandas DataFrame - prod() function
The Pandas DataFrame prod() function returns the product of the values over the specified axis. The syntax for using this function is mentioned below:
Syntax
DataFrame.prod(axis=None, skipna=None, level=None, numeric_only=None, min_count=0)
Parameters
axis |
Optional. Specify {0 or 'index', 1 or 'columns'}. If 0 or 'index', products are generated for each column. If 1 or 'columns', products are generated for each row. Default: 0 |
skipna |
Optional. Specify True to exclude NA/null values when computing the result. Default is True. |
level |
Optional. Specify level (int or str). If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. A str specifies the level name. |
numeric_only |
Optional. Specify True to include only float, int or boolean data. Default: False |
min_count |
Optional. Specify required number of valid values to perform the operation. If the count of non-NA values is less than the min_count, the result will be NA. |
Return Value
Returns product of Series or DataFrame if a level is specified.
Example: using prod() column-wise on whole DataFrame
In the example below, a DataFrame df is created. The prod() function is used to get the product of each column.
import pandas as pd import numpy as np df = pd.DataFrame({ "Sample1": [1, 2, 3, 4, 5], "Sample2": [11, 12, 13, 14, 15]}, index= ["x1", "x2", "x3", "x4", "x5"] ) print("The DataFrame is:") print(df) #Product of all entries column-wise print("\ndf.prod() returns:") print(df.prod())
The output of the above code will be:
The DataFrame is: Sample1 Sample2 x1 1 11 x2 2 12 x3 3 13 x4 4 14 x5 5 15 df.prod() returns: Sample1 120 Sample2 360360 dtype: int64
Example: using prod() row-wise on whole DataFrame
To get the row-wise product, the axis parameter can be set to 1.
import pandas as pd import numpy as np df = pd.DataFrame({ "Sample1": [1, 2, 3, 4, 5], "Sample2": [11, 12, 13, 14, 15]}, index= ["x1", "x2", "x3", "x4", "x5"] ) print("The DataFrame is:") print(df) #Product of all entries row-wise print("\ndf.prod(axis=1) returns:") print(df.prod(axis=1))
The output of the above code will be:
The DataFrame is: Sample1 Sample2 x1 1 11 x2 2 12 x3 3 13 x4 4 14 x5 5 15 df.prod(axis=1) returns: x1 11 x2 24 x3 39 x4 56 x5 75 dtype: int64
Example: using prod() on selected column
Instead of whole DataFrame, the prod() function can be applied on selected columns. Consider the following example.
import pandas as pd import numpy as np df = pd.DataFrame({ "Sample1": [1, 2, 3, 4, 5], "Sample2": [11, 12, 13, 14, 15], "Sample3": [9, 8, 7, 6, 5]}, index= ["x1", "x2", "x3", "x4", "x5"] ) print("The DataFrame is:") print(df) #product of single column print("\ndf['Sample1'].prod() returns:") print(df["Sample1"].prod()) #product of multiple columns print("\ndf[['Sample1', 'Sample3']].prod() returns:") print(df[["Sample1", "Sample3"]].prod())
The output of the above code will be:
The DataFrame is: Sample1 Sample2 Sample3 x1 1 11 9 x2 2 12 8 x3 3 13 7 x4 4 14 6 x5 5 15 5 df['Sample1'].prod() returns: 120 df[['Sample1', 'Sample3']].prod() returns: Sample1 120 Sample3 15120 dtype: int64
❮ Pandas DataFrame - Functions