Skip to content

Commit 7d0980c

Browse files
committed
DOC: Clarify groupby operates on axis 0 and remove 'selected axis' reference
This commit addresses issue #56397 by removing outdated references to "the selected axis" in groupby documentation and clarifying that: 1. DataFrame.groupby() always operates along axis 0 (rows) 2. The axis parameter was removed in pandas 3.0 3. To group by columns, users must transpose the DataFrame first Changes: - Updated API reference docstring in DataFrame.groupby() to replace "selected axis" with "number of rows" - Enhanced user guide to explicitly state groupby operates on axis 0 - Added note explaining the removal of the axis parameter and the need to use .T for column-wise grouping Fixes #56397
1 parent ea75dd7 commit 7d0980c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

doc/source/user_guide/groupby.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ We could naturally group by either the ``A`` or ``B`` columns, or both:
137137

138138
``df.groupby('A')`` is just syntactic sugar for ``df.groupby(df['A'])``.
139139

140-
The above GroupBy will split the DataFrame on its index (rows). To split by columns, first do
141-
a transpose:
140+
The above GroupBy will split the DataFrame on its index (rows). DataFrame groupby
141+
always operates along axis 0 (rows). To split by columns instead, first transpose
142+
the DataFrame:
142143

143144
.. ipython::
144145

@@ -151,6 +152,11 @@ a transpose:
151152

152153
In [5]: grouped = df.T.groupby(get_letter_type)
153154

155+
.. note::
156+
157+
Prior to pandas 3.0, groupby had an ``axis`` parameter. This has been removed.
158+
To group by columns, transpose your DataFrame using ``.T`` before calling groupby.
159+
154160
pandas :class:`~pandas.Index` objects support duplicate values. If a
155161
non-unique index is used as the group key in a groupby operation, all values
156162
for the same index value will be considered to be in one group and thus the

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9432,7 +9432,7 @@ def groupby(
94329432
index. If a dict or Series is passed, the Series or dict VALUES
94339433
will be used to determine the groups (the Series' values are first
94349434
aligned; see ``.align()`` method). If a list or ndarray of length
9435-
equal to the selected axis is passed (see the `groupby user guide
9435+
equal to the number of rows is passed (see the `groupby user guide
94369436
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#splitting-an-object-into-groups>`_),
94379437
the values are used as-is to determine the groups. A label or list
94389438
of labels may be passed to group by the columns in ``self``.

0 commit comments

Comments
 (0)