Skip to content

Named agg tuples Issue #18220 #54563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/user_guide/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ no column selection, so the values are just the functions.
max_height="max",
)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Applying different functions to DataFrame columns
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
22 changes: 22 additions & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,28 @@ class DataFrameGroupBy(GroupBy[DataFrame]):
A
1 1.0
2 3.0

Passing a List of Tuples

Demonstrates using the `agg` method with a list of tuples for grouping and aggregation.
Consider a DataFrame `df`:
key | data
----|------
'a' | 0.5
'a' | -1.0
'b' | 2.0
'b' | -0.5
'a' | 1.2

Example: Applying multiple aggregations - mean and standard deviation
df_result = df.groupby('key')['data'].agg([('mean_value', 'mean'), ('std_deviation', 'std')])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you format this as a code example like the others in this string?


Using a list of tuples provides a concise way to apply multiple aggregations to the same column while controlling
the output column names. This approach is especially handy when you need to calculate various statistics on
the same data within each group.

:return: Example of using the `agg` method with a list of tuples for grouping and aggregation.
:rtype: None
"""
)

Expand Down