Pandas DataFrame Product Method

PythonPythonBeginner
Practice Now

Introduction

The DataFrame.product() method in Pandas is used to find the product of the values in a DataFrame. It can be applied along either the index or column axis and returns a Series or DataFrame.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/booleans -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/lists -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/tuples -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/dictionaries -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/importing_modules -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/numerical_computing -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/data_analysis -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} python/build_in_functions -.-> lab-68701{{"`Pandas DataFrame Product Method`"}} end

Import the necessary libraries and create a DataFrame

Let's start by importing the pandas library and creating a DataFrame, df_1.

#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)

Find the product along the index axis

To find the product along the index axis, set axis=0 in the DataFrame.product() method.

print("---------------------------------")
print(df_1.product(axis=0))

Find the product along the column axis

To find the product along the column axis, set axis=1 in the DataFrame.product() method.

print("---------------------------------")
print(df_1.product(axis=1))

Include null values in the product calculation

By default, the DataFrame.product() method excludes null or missing values. To include them, set skipna=False in the method.

print("---------------------------------")
print(df_1.product(axis=1, skipna=False))

Summary

In this lab, we learned how to use the DataFrame.product() method in Pandas to find the product of values in a DataFrame. We saw how to apply it along the index and column axis, and how to include or exclude null values in the calculation. This method is useful for performing mathematical computations on DataFrame values.

Other Python Tutorials you may like