Signup/Sign In

Pandas DataFrame prod() Method

In Pandas, to find the product of the DataFrame values, we can use the DataFrame.prod() method. When we apply this method to the DataFrame, it returns the Series or DataFrame that consists of the product of the values over the required axis.

The below is the syntax of the DataFrame.prod() method.

Syntax

DataFrame.prod(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)

Parameters

axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0, method applied over the index axis and when the axis=1 method applied over the column axis.

skipna: bool(True or False). The default value is None. If this parameter is True, it excludes all NA/null values when computing the result.

level: It represents the int or level name, the default value is None. If the axis is a MultiIndex (hierarchical), count along with a particular level, collapsing into a Series.

numeric_only: bool(True or False), the default is None. If this parameter is True, it includes only float, int, boolean columns.

min_count: It represents the int, and the default value is 0. It indicates the required number of valid values to perform the operation.

**kwargs: Additional keyword arguments to be passed to the method.

Example: Finding the product of the DataFrame in Pandas

Here, we are finding the product of the DataFrame values along the index axis using the DataFrame.prod() method. It will return a product of all the values of the dataframe.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
print("------The DataFrame is---------")
print(df_1)
print("---------------------------------")
print(df_1.prod(axis=0))


------The DataFrame is---------
A B C
0 1 4 7
1 2 5 8
2 3 6 9
---------------------------------
A 6
B 120
C 504
dtype: int64

Example: Finding the product of the DataFrame in Pandas

Here, we are finding the product of the DataFrame values along the column axis using the DataFrame.prod() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
print("------The DataFrame is---------")
print(df_1)
print("---------------------------------")
print(df_1.prod(axis=1))


------The DataFrame is---------
A B C
0 1 4 7
1 2 5 8
2 3 6 9
---------------------------------
0 28
1 80
2 162
dtype: int64

Example 3: Finding the product of the DataFrame having null

By default, the DataFrame.prod() method excludes the null or missing values when performing the product operation. If we set skipna=False in the DataFrame.prod() method, it includes the null values while performing the product operation. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df_1=pd.DataFrame({'A':[1,2,np.nan],'B':[np.nan,5,6],'C':[7,8,9]})
print("------The DataFrame is---------")
print(df_1)
print("---------------------------------")
print(df_1.prod(axis=1,skipna=False))


------The DataFrame is---------
A B C
0 1.0 NaN 7
1 2.0 5.0 8
2 NaN 6.0 9
---------------------------------
0 NaN
1 80.0
2 NaN
dtype: float64

Conclusion

In this tutorial, we learned the Python pandas DataFrame.prod() method. We learned the syntax, parameters and applied this method on the DataFrame.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.