Skip to content

Commit 8caac83

Browse files
authored
Merge branch 'main' into pd-cut-doc
2 parents 54d2058 + 18a3eec commit 8caac83

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

doc/source/user_guide/groupby.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,9 @@ column, which produces an aggregated result with a hierarchical column index:
668668
grouped[["C", "D"]].agg(["sum", "mean", "std"])
669669
670670
671-
The resulting aggregations are named after the functions themselves. If you
672-
need to rename, then you can add in a chained operation for a ``Series`` like this:
671+
The resulting aggregations are named after the functions themselves.
672+
673+
For a ``Series``, if you need to rename, you can add in a chained operation like this:
673674

674675
.. ipython:: python
675676
@@ -679,8 +680,19 @@ need to rename, then you can add in a chained operation for a ``Series`` like th
679680
.rename(columns={"sum": "foo", "mean": "bar", "std": "baz"})
680681
)
681682
683+
Or, you can simply pass a list of tuples each with the name of the new column and the aggregate function:
684+
685+
.. ipython:: python
686+
687+
(
688+
grouped["C"]
689+
.agg([("foo", "sum"), ("bar", "mean"), ("baz", "std")])
690+
)
691+
682692
For a grouped ``DataFrame``, you can rename in a similar manner:
683693

694+
By chaining ``rename`` operation,
695+
684696
.. ipython:: python
685697
686698
(
@@ -689,6 +701,16 @@ For a grouped ``DataFrame``, you can rename in a similar manner:
689701
)
690702
)
691703
704+
Or, passing a list of tuples,
705+
706+
.. ipython:: python
707+
708+
(
709+
grouped[["C", "D"]].agg(
710+
[("foo", "sum"), ("bar", "mean"), ("baz", "std")]
711+
)
712+
)
713+
692714
.. note::
693715

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

0 commit comments

Comments
 (0)