Pandas DataFrame product() Method
In Pandas, to find the product of the DataFrame values, we can use the DataFrame.product()
method.
In this tutorial, we will discuss and learn this method with examples. When we apply this method to the DataFrame, it returns a Series or DataFrame that consists of the product of the values over the required axis.
The below is the syntax of the DataFrame.product()
method.
Syntax
DataFrame.product(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
, function applied over the index
axis and when the axis=1
function 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 using DataFrame.product()
Method
Here, we are finding the product of the DataFrame values along the index axis using the DataFrame.product()
method. To get product of index axis, just set axis=0 in the method.
#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.product(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 2: Finding the product using DataFrame.product()
Method
Here, we are finding the product of the DataFrame values along the column axis using the DataFrame.product()
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.product(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: Finding the product using DataFrame.product()
Method
By default DataFrame.product()
method excludes the null or missing values when performing the product operation. If we set skipna=False
in the DataFrame.product()
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.product(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.product()
method. We learned the syntax, parameters and applied this method on the DataFrame to understood the DataFrame.product()
method.