@@ -539,19 +539,19 @@ Some common aggregating functions are tabulated below:
539
539
:widths: 20, 80
540
540
:delim: ;
541
541
542
- :meth: `~pd.core.groupby.DataFrameGroupBy.mean `;Compute mean of groups
543
- :meth: `~pd.core.groupby.DataFrameGroupBy.sum `;Compute sum of group values
544
- :meth: `~pd.core.groupby.DataFrameGroupBy.size `;Compute group sizes
545
- :meth: `~pd.core.groupby.DataFrameGroupBy.count `;Compute count of group
546
- :meth: `~pd.core.groupby.DataFrameGroupBy.std `;Standard deviation of groups
547
- :meth: `~pd.core.groupby.DataFrameGroupBy.var `;Compute variance of groups
548
- :meth: `~pd.core.groupby.DataFrameGroupBy.sem `;Standard error of the mean of groups
549
- :meth: `~pd.core.groupby.DataFrameGroupBy.describe `;Generates descriptive statistics
550
- :meth: `~pd.core.groupby.DataFrameGroupBy.first `;Compute first of group values
551
- :meth: `~pd.core.groupby.DataFrameGroupBy.last `;Compute last of group values
552
- :meth: `~pd.core.groupby.DataFrameGroupBy.nth `;Take nth value, or a subset if n is a list
553
- :meth: `~pd.core.groupby.DataFrameGroupBy.min `;Compute min of group values
554
- :meth: `~pd.core.groupby.DataFrameGroupBy.max `;Compute max of group values
542
+ :meth: `~pd.core.groupby.DataFrameGroupBy.mean `;Compute mean of groups
543
+ :meth: `~pd.core.groupby.DataFrameGroupBy.sum `;Compute sum of group values
544
+ :meth: `~pd.core.groupby.DataFrameGroupBy.size `;Compute group sizes
545
+ :meth: `~pd.core.groupby.DataFrameGroupBy.count `;Compute count of group
546
+ :meth: `~pd.core.groupby.DataFrameGroupBy.std `;Standard deviation of groups
547
+ :meth: `~pd.core.groupby.DataFrameGroupBy.var `;Compute variance of groups
548
+ :meth: `~pd.core.groupby.DataFrameGroupBy.sem `;Standard error of the mean of groups
549
+ :meth: `~pd.core.groupby.DataFrameGroupBy.describe `;Generates descriptive statistics
550
+ :meth: `~pd.core.groupby.DataFrameGroupBy.first `;Compute first of group values
551
+ :meth: `~pd.core.groupby.DataFrameGroupBy.last `;Compute last of group values
552
+ :meth: `~pd.core.groupby.DataFrameGroupBy.nth `;Take nth value, or a subset if n is a list
553
+ :meth: `~pd.core.groupby.DataFrameGroupBy.min `;Compute min of group values
554
+ :meth: `~pd.core.groupby.DataFrameGroupBy.max `;Compute max of group values
555
555
556
556
557
557
The aggregating functions above will exclude NA values. Any function which
@@ -1052,7 +1052,14 @@ Some operations on the grouped data might not fit into either the aggregate or
1052
1052
transform categories. Or, you may simply want GroupBy to infer how to combine
1053
1053
the results. For these, use the ``apply `` function, which can be substituted
1054
1054
for both ``aggregate `` and ``transform `` in many standard use cases. However,
1055
- ``apply `` can handle some exceptional use cases, for example:
1055
+ ``apply `` can handle some exceptional use cases.
1056
+
1057
+ .. note ::
1058
+
1059
+ ``apply `` can act as a reducer, transformer, *or * filter function, depending
1060
+ on exactly what is passed to it. It can depend on the passed function and
1061
+ exactly what you are grouping. Thus the grouped column(s) may be included in
1062
+ the output as well as set the indices.
1056
1063
1057
1064
.. ipython :: python
1058
1065
@@ -1064,16 +1071,14 @@ for both ``aggregate`` and ``transform`` in many standard use cases. However,
1064
1071
1065
1072
The dimension of the returned result can also change:
1066
1073
1067
- .. ipython ::
1068
-
1069
- In [8]: grouped = df.groupby('A')['C']
1074
+ .. ipython :: python
1070
1075
1071
- In [10]: def f(group):
1072
- ....: return pd.DataFrame({'original': group,
1073
- ....: 'demeaned': group - group.mean()})
1074
- ....:
1076
+ grouped = df.groupby(' A' )[' C' ]
1075
1077
1076
- In [11]: grouped.apply(f)
1078
+ def f (group ):
1079
+ return pd.DataFrame({' original' : group,
1080
+ ' demeaned' : group - group.mean()})
1081
+ grouped.apply(f)
1077
1082
1078
1083
``apply `` on a Series can operate on a returned value from the applied function,
1079
1084
that is itself a series, and possibly upcast the result to a DataFrame:
@@ -1088,11 +1093,33 @@ that is itself a series, and possibly upcast the result to a DataFrame:
1088
1093
s
1089
1094
s.apply(f)
1090
1095
1096
+ Control grouped column(s) placement with ``group_keys ``
1097
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1098
+
1091
1099
.. note ::
1092
1100
1093
- ``apply `` can act as a reducer, transformer, *or * filter function, depending on exactly what is passed to it.
1094
- So depending on the path taken, and exactly what you are grouping. Thus the grouped columns(s) may be included in
1095
- the output as well as set the indices.
1101
+ If ``group_keys=True `` is specified when calling :meth: `~DataFrame.groupby `,
1102
+ functions passed to ``apply `` that return like-indexed outputs will have the
1103
+ group keys added to the result index. Previous versions of pandas would add
1104
+ the group keys only when the result from the applied function had a different
1105
+ index than the input. If ``group_keys `` is not specified, the group keys will
1106
+ not be added for like-indexed outputs. In the future this behavior
1107
+ will change to always respect ``group_keys ``, which defaults to ``True ``.
1108
+
1109
+ .. versionchanged :: 1.5.0
1110
+
1111
+ To control whether the grouped column(s) are included in the indices, you can use
1112
+ the argument ``group_keys ``. Compare
1113
+
1114
+ .. ipython :: python
1115
+
1116
+ df.groupby(" A" , group_keys = True ).apply(lambda x : x)
1117
+
1118
+ with
1119
+
1120
+ .. ipython :: python
1121
+
1122
+ df.groupby(" A" , group_keys = False ).apply(lambda x : x)
1096
1123
1097
1124
Similar to :ref: `groupby.aggregate.udfs `, the resulting dtype will reflect that of the
1098
1125
apply function. If the results from different groups have different dtypes, then
0 commit comments