Skip to content

Commit d242789

Browse files
[Term Entry] Python - Pandas groupBy: prod() (#7711)
* Adding groupby.prod * Adding groupby.prod * minor fixes * Update prod.md * Update content/pandas/concepts/groupby/terms/prod/prod.md * Update content/pandas/concepts/groupby/terms/prod/prod.md ---------
1 parent ea7f33f commit d242789

File tree

1 file changed

+91
-0
lines changed
  • content/pandas/concepts/groupby/terms/prod

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
Title: '.prod()'
3+
Description: 'Produces a new Series or DataFrame by computing the product of the values within the group.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Data Structures'
9+
- 'Pandas'
10+
CatalogContent:
11+
- 'learn-python-3'
12+
- 'paths/data-science'
13+
---
14+
15+
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.
16+
17+
## Syntax
18+
19+
```pseudo
20+
groupbyobject.prod(numeric_only=False, min_count=0)
21+
```
22+
23+
**Parameters:**
24+
25+
- `numeric_only`: If `True`, non-numeric columns are excluded. If `False`, attempts to include all columns (non-numeric columns are ignored in computation).
26+
- `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`.
27+
28+
**Return value:**
29+
30+
Returns a `DataFrame` (or `Series` if applied on a SeriesGroupBy object) containing the product of each numeric column for each group.
31+
32+
## Example
33+
34+
The following example produces a `GroupBy` object from a `DataFrame` and executes the `.prod()` method on it:
35+
36+
```py
37+
import pandas as pd
38+
39+
df = pd.DataFrame({
40+
'Key' : ['A', 'A', 'B', 'B', 'C', 'C','D'],
41+
'Value1' : [2, 3, 4, 5, 6, 9, 10],
42+
'Value2' : [10, 5, 2, 3, 4, 2, 11]
43+
})
44+
print(df, end='\n\n')
45+
46+
group_prod = df.groupby('Key').prod()
47+
48+
print(group_prod)
49+
```
50+
51+
This example produces the following output:
52+
53+
```shell
54+
Key Value1 Value2
55+
0 A 2 10
56+
1 A 3 5
57+
2 B 4 2
58+
3 B 5 3
59+
4 C 6 4
60+
5 C 9 2
61+
6 D 10 11
62+
63+
Value1 Value2
64+
Key
65+
A 6 50
66+
B 20 6
67+
C 54 8
68+
D 10 11
69+
```
70+
71+
## Codebyte Example
72+
73+
This example computes the product of all prices and the product of all quantities within each category:
74+
75+
```codebyte/python
76+
import pandas as pd
77+
78+
# Sample sales data
79+
df = pd.DataFrame({
80+
'Category': ['Electronics', 'Electronics', 'Clothing', 'Clothing', 'Books'],
81+
'Price': [200, 150, 50, 30, 20],
82+
'Quantity': [2, 3, 4, 5, 10]
83+
})
84+
85+
print("Original DataFrame:\n", df, end='\n\n')
86+
87+
# Group by Category and compute the product of numeric columns
88+
category_prod = df.groupby('Category').prod()
89+
90+
print("Grouped product:\n", category_prod)
91+
```

0 commit comments

Comments
 (0)