Skip to content

Commit

Permalink
DOC: DataFrame.groupy.agg with a list of tuples (pandas-dev#59282)
Browse files Browse the repository at this point in the history
Add doc for groupby.agg with a list of tuples

Co-authored-by: hye ryung cho <[email protected]>
  • Loading branch information
jahn96 and cho0209 authored Jul 19, 2024
1 parent abcf477 commit 18a3eec
Showing 1 changed file with 24 additions and 2 deletions.
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

0 comments on commit 18a3eec

Please sign in to comment.