From 4c6ab167dafdd88ebcbe84be6468edd7b819bcc9 Mon Sep 17 00:00:00 2001 From: SrikartikMateti Date: Thu, 9 Oct 2025 18:56:12 +0530 Subject: [PATCH 1/4] Adding groupby.prod --- .../concepts/groupby/terms/prod/prod.md | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 content/pandas/concepts/groupby/terms/prod/prod.md diff --git a/content/pandas/concepts/groupby/terms/prod/prod.md b/content/pandas/concepts/groupby/terms/prod/prod.md new file mode 100644 index 00000000000..020de2b3cd1 --- /dev/null +++ b/content/pandas/concepts/groupby/terms/prod/prod.md @@ -0,0 +1,81 @@ +--- +Title: '.prod()' +Description: 'Produces a new Series or DataFrame by computing the product of the values within the group.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Data Structures' + - 'Pandas' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`.prod()`** method produces a new `Series` or [`DataFrame`](https://www.codecademy.com/resources/docs/pandas/dataframe) with the product of the values in a [`GroupBy`](https://www.codecademy.com/resources/docs/pandas/groupby) object. + +## Syntax + +```pseudo +groupbyobject.prod(numeric_only, min_count) +``` + +The `.prod()` method has the following parameters: + +- `numeric_only`: Boolean value. `True` includes only int, float, and boolean columns. Default value is `False`. +- `min_count`: Int value. Required number of valid entries in order to produce a result. Default value is 0. + +## Example + +The following example produces a `GroupBy` object from a [`DataFrame`](https://www.codecademy.com/resources/docs/pandas/dataframe) and executes the `.prod()` method on it. + +```py +import pandas as pd + +df = pd.DataFrame({ + 'Key' : ['A', 'A', 'B', 'B', 'C', 'C'], + 'Value1' : [2, 3, 4, 5, 6, 9], + 'Value2' : [10, 5, 2, 3, 4, 2] +}) +print(df, end='\n\n') + +group_prod = df.groupby('Key').prod() + +print(group_prod) +``` + +This example produces the following output: + +```shell + Key Value1 Value2 +0 A 2 10 +1 A 3 5 +2 B 4 2 +3 B 5 3 +4 C 6 4 +5 C 9 2 + + Value1 Value2 +Key +A 6 50 +B 20 6 +C 54 8 +``` + +## Codebyte Example + +Use the Codebytes editor below to produce a new DataFrame with the `.prod()` method. +```codebyte/py +import pandas as pd + +df = pd.DataFrame({ + 'Key' : ['A', 'A', 'B', 'B', 'C', 'C'], + 'Value1' : [2, 3, 4, 5, 6, 9], + 'Value2' : [10, 5, 2, 3, 4, 2] +}) +print(df, end='\n\n') + +group_prod = df.groupby('Key').prod() + +print(group_prod) +``` \ No newline at end of file From 08a191fc69779e8200c88e54ce4fc9c024baf9a9 Mon Sep 17 00:00:00 2001 From: SrikartikMateti Date: Thu, 9 Oct 2025 19:29:57 +0530 Subject: [PATCH 2/4] Adding groupby.prod --- content/pandas/concepts/groupby/terms/prod/prod.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/content/pandas/concepts/groupby/terms/prod/prod.md b/content/pandas/concepts/groupby/terms/prod/prod.md index 020de2b3cd1..a27ad19159f 100644 --- a/content/pandas/concepts/groupby/terms/prod/prod.md +++ b/content/pandas/concepts/groupby/terms/prod/prod.md @@ -33,9 +33,9 @@ The following example produces a `GroupBy` object from a [`DataFrame`](https://w import pandas as pd df = pd.DataFrame({ - 'Key' : ['A', 'A', 'B', 'B', 'C', 'C'], - 'Value1' : [2, 3, 4, 5, 6, 9], - 'Value2' : [10, 5, 2, 3, 4, 2] + 'Key' : ['A', 'A', 'B', 'B', 'C', 'C','D'], + 'Value1' : [2, 3, 4, 5, 6, 9, 10], + 'Value2' : [10, 5, 2, 3, 4, 2, 11] }) print(df, end='\n\n') @@ -54,12 +54,14 @@ This example produces the following output: 3 B 5 3 4 C 6 4 5 C 9 2 +6 D 10 11 Value1 Value2 Key A 6 50 B 20 6 C 54 8 +D 10 11 ``` ## Codebyte Example @@ -69,9 +71,9 @@ Use the Codebytes editor below to produce a new DataFrame with the `.prod()` met import pandas as pd df = pd.DataFrame({ - 'Key' : ['A', 'A', 'B', 'B', 'C', 'C'], - 'Value1' : [2, 3, 4, 5, 6, 9], - 'Value2' : [10, 5, 2, 3, 4, 2] + 'Key' : ['A', 'A', 'B', 'B', 'C', 'C','D'], + 'Value1' : [2, 3, 4, 5, 6, 9, 10], + 'Value2' : [10, 5, 2, 3, 4, 2, 11] }) print(df, end='\n\n') From 26823048cb081331674d996519618f96cca824ec Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 10 Oct 2025 15:21:20 +0530 Subject: [PATCH 3/4] minor fixes --- .../concepts/groupby/terms/prod/prod.md | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/content/pandas/concepts/groupby/terms/prod/prod.md b/content/pandas/concepts/groupby/terms/prod/prod.md index a27ad19159f..d4d4d6b0713 100644 --- a/content/pandas/concepts/groupby/terms/prod/prod.md +++ b/content/pandas/concepts/groupby/terms/prod/prod.md @@ -20,14 +20,18 @@ The **`.prod()`** method produces a new `Series` or [`DataFrame`](https://www.co groupbyobject.prod(numeric_only, min_count) ``` -The `.prod()` method has the following parameters: +**Parameters:** -- `numeric_only`: Boolean value. `True` includes only int, float, and boolean columns. Default value is `False`. -- `min_count`: Int value. Required number of valid entries in order to produce a result. Default value is 0. +- `numeric_only`: If `True`, non-numeric columns are excluded. If `False`, attempts to include all columns (non-numeric columns are ignored in computation). +- `min_count`: If the number of valid (non-NA) entries in a group is less than `min_count`, the result for that group is `NaN`. + +**Return value:** + +Returns a `DataFrame` (or `Series` if applied on a SeriesGroupBy object) containing the product of each numeric column for each group. ## Example -The following example produces a `GroupBy` object from a [`DataFrame`](https://www.codecademy.com/resources/docs/pandas/dataframe) and executes the `.prod()` method on it. +The following example produces a `GroupBy` object from a [`DataFrame`](https://www.codecademy.com/resources/docs/pandas/dataframe) and executes the `.prod()` method on it: ```py import pandas as pd @@ -57,7 +61,7 @@ This example produces the following output: 6 D 10 11 Value1 Value2 -Key +Key A 6 50 B 20 6 C 54 8 @@ -66,18 +70,22 @@ D 10 11 ## Codebyte Example -Use the Codebytes editor below to produce a new DataFrame with the `.prod()` method. -```codebyte/py +This example calculates the total sales value for each product category by multiplying the price and quantity sold. It demonstrates how `.prod()` can be used in a real-world grouped dataset: + +```codebyte/python import pandas as pd +# Sample sales data df = pd.DataFrame({ - 'Key' : ['A', 'A', 'B', 'B', 'C', 'C','D'], - 'Value1' : [2, 3, 4, 5, 6, 9, 10], - 'Value2' : [10, 5, 2, 3, 4, 2, 11] + 'Category': ['Electronics', 'Electronics', 'Clothing', 'Clothing', 'Books'], + 'Price': [200, 150, 50, 30, 20], + 'Quantity': [2, 3, 4, 5, 10] }) -print(df, end='\n\n') -group_prod = df.groupby('Key').prod() +print("Original DataFrame:\n", df, end='\n\n') -print(group_prod) -``` \ No newline at end of file +# Group by Category and compute the product of numeric columns +category_prod = df.groupby('Category').prod() + +print("Grouped product:\n", category_prod) +``` From 4cd59459f5fbdf3a961a1a5358e3b65a8412002a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 10 Oct 2025 15:22:27 +0530 Subject: [PATCH 4/4] Update prod.md --- content/pandas/concepts/groupby/terms/prod/prod.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pandas/concepts/groupby/terms/prod/prod.md b/content/pandas/concepts/groupby/terms/prod/prod.md index d4d4d6b0713..ffb3d107957 100644 --- a/content/pandas/concepts/groupby/terms/prod/prod.md +++ b/content/pandas/concepts/groupby/terms/prod/prod.md @@ -31,7 +31,7 @@ Returns a `DataFrame` (or `Series` if applied on a SeriesGroupBy object) contain ## Example -The following example produces a `GroupBy` object from a [`DataFrame`](https://www.codecademy.com/resources/docs/pandas/dataframe) and executes the `.prod()` method on it: +The following example produces a `GroupBy` object from a `DataFrame` and executes the `.prod()` method on it: ```py import pandas as pd