Skip to content

Commit d999923

Browse files
committed
doc
1 parent 5ff65d6 commit d999923

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

pandas/core/groupby/generic.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@
115115
@set_module("pandas")
116116
class NamedAgg(tuple):
117117
"""
118-
Helper for defining named aggregations in groupby operations.
118+
Helper for defining named aggregations in ``DataFrame.groupby().agg``.
119119
120-
Subclass of tuple that wraps an aggregation function.
120+
Use ``pd.NamedAgg`` to specify column-specific aggregations with explicit
121+
output names.
121122
122123
Parameters
123124
----------
@@ -126,41 +127,37 @@ class NamedAgg(tuple):
126127
aggfunc : function or str
127128
Function to apply to the provided column. If string, the name of a built-in
128129
pandas function.
129-
*args : tuple, optional
130-
Positional arguments to pass to `aggfunc` when it is called.
131-
**kwargs : dict, optional
132-
Keyword arguments to pass to `aggfunc` when it is called.
130+
*args, **kwargs :
131+
Optional positional and keyword arguments passed to ``aggfunc``.
133132
134133
See Also
135134
--------
136135
DataFrame.groupby : Group DataFrame using a mapper or by a Series of columns.
137136
138137
Examples
139138
--------
140-
>>> df = pd.DataFrame({"key": [1, 1, 2], "a": [-1, 0, 1], 1: [10, 11, 12]})
139+
>>> df = pd.DataFrame({"key": [1, 1, 2], "a": [-1, 0, 1], "b": [10, 11, 12]})
141140
>>> agg_a = pd.NamedAgg(column="a", aggfunc="min")
142-
>>> agg_1 = pd.NamedAgg(column=1, aggfunc=lambda x: np.mean(x))
143-
>>> df.groupby("key").agg(result_a=agg_a, result_1=agg_1)
144-
result_a result_1
141+
>>> agg_b = pd.NamedAgg(column="b", aggfunc=lambda x: x.mean())
142+
>>> df.groupby("key").agg(result_a=agg_a, result_b=agg_b)
143+
result_a result_b
145144
key
146145
1 -1 10.5
147146
2 1 12.0
148147
149-
def n_between(ser, low, high, **kwargs):
150-
return ser.between(low, high, **kwargs).sum()
148+
>>> def n_between(ser, low, high, **kwargs):
149+
... return ser.between(low, high, **kwargs).sum()
151150
152-
Using positional arguments
153-
agg_between = pd.NamedAgg("a", n_between, 0, 1)
154-
df.groupby("key").agg(count_between=agg_between)
155-
count_between
151+
>>> agg_between = pd.NamedAgg("a", n_between, 0, 1)
152+
>>> df.groupby("key").agg(count_between=agg_between)
153+
count_between
156154
key
157155
1 1
158156
2 1
159157
160-
Using both positional and keyword arguments
161-
agg_between_kw = pd.NamedAgg("a", n_between, 0, 1, inclusive="both")
162-
df.groupby("key").agg(count_between_kw=agg_between_kw)
163-
count_between_kw
158+
>>> agg_between_kw = pd.NamedAgg("a", n_between, 0, 1, inclusive="both")
159+
>>> df.groupby("key").agg(count_between_kw=agg_between_kw)
160+
count_between_kw
164161
key
165162
1 1
166163
2 1

0 commit comments

Comments
 (0)