Skip to content

DOC: DataFrame.groupy.agg with a list of tuples #59282

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

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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
26 changes: 24 additions & 2 deletions doc/source/user_guide/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,9 @@ column, which produces an aggregated result with a hierarchical column index:
grouped[["C", "D"]].agg(["sum", "mean", "std"])


The resulting aggregations are named after the functions themselves. If you
need to rename, then you can add in a chained operation for a ``Series`` like this:
The resulting aggregations are named after the functions themselves.

For a ``Series``, if you need to rename, you can add in a chained operation like this:

.. ipython:: python

Expand All @@ -679,8 +680,19 @@ need to rename, then you can add in a chained operation for a ``Series`` like th
.rename(columns={"sum": "foo", "mean": "bar", "std": "baz"})
)

Or, you can simply pass a list of tuples each with the name of the new column and the aggregate function:

.. ipython:: python

(
grouped["C"]
.agg([("foo", "sum"), ("bar", "mean"), ("baz", "std")])
)

For a grouped ``DataFrame``, you can rename in a similar manner:

By chaining ``rename`` operation,

.. ipython:: python

(
Expand All @@ -689,6 +701,16 @@ For a grouped ``DataFrame``, you can rename in a similar manner:
)
)

Or, passing a list of tuples,

.. ipython:: python

(
grouped[["C", "D"]].agg(
[("foo", "sum"), ("bar", "mean"), ("baz", "std")]
)
)

.. note::

In general, the output column names should be unique, but pandas will allow
Expand Down