Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions doc/source/user_guide/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ We could naturally group by either the ``A`` or ``B`` columns, or both:

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

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

.. ipython::

Expand All @@ -151,6 +152,11 @@ a transpose:

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

.. note::

Prior to pandas 3.0, groupby had an ``axis`` parameter. This has been removed.
To group by columns, transpose your DataFrame using ``.T`` before calling groupby.

pandas :class:`~pandas.Index` objects support duplicate values. If a
non-unique index is used as the group key in a groupby operation, all values
for the same index value will be considered to be in one group and thus the
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9432,7 +9432,7 @@ def groupby(
index. If a dict or Series is passed, the Series or dict VALUES
will be used to determine the groups (the Series' values are first
aligned; see ``.align()`` method). If a list or ndarray of length
equal to the selected axis is passed (see the `groupby user guide
equal to the number of rows is passed (see the `groupby user guide
<https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#splitting-an-object-into-groups>`_),
the values are used as-is to determine the groups. A label or list
of labels may be passed to group by the columns in ``self``.
Expand Down
Loading