Skip to content

Commit 4ae33be

Browse files
committed
Add examples for Tuples DataFrameGroupBy class
1 parent 8a962d0 commit 4ae33be

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pandas/core/groupby/generic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,29 @@ def unique(self) -> Series:
13341334

13351335

13361336
class DataFrameGroupBy(GroupBy[DataFrame]):
1337+
groupby_agg_with_tuples_example = """
1338+
Demonstrates using the `agg` method with a list of tuples for grouping and aggregation.
1339+
1340+
Consider a DataFrame `df`:
1341+
1342+
key | data
1343+
----|------
1344+
'a' | 0.5
1345+
'a' | -1.0
1346+
'b' | 2.0
1347+
'b' | -0.5
1348+
'a' | 1.2
1349+
1350+
Example: Applying multiple aggregations - mean and standard deviation
1351+
df_result = df.groupby('key')['data'].agg([('mean_value', 'mean'), ('std_deviation', 'std')])
1352+
1353+
Using a list of tuples provides a concise way to apply multiple aggregations to the same column while controlling
1354+
the output column names. This approach is especially handy when you need to calculate various statistics on
1355+
the same data within each group.
1356+
1357+
:return: Example of using the `agg` method with a list of tuples for grouping and aggregation.
1358+
:rtype: None
1359+
"""
13371360
_agg_examples_doc = dedent(
13381361
"""
13391362
Examples

0 commit comments

Comments
 (0)