Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the addition of "always" here, clarifying that it isn't just the example above. However these two sentences seem redundant:

DataFrame groupby always operates along axis 0 (rows).

and

The above GroupBy will split the DataFrame on its index (rows).

Can you combine them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I don't see any significant difference between

To split by columns, first do a transpose:

and

To split by columns instead, first transpose the DataFrame:

This seems to me to just be a matter of personal style or taste. I generally think we should not make changes unless they are objectively positive.


.. 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to repeat it again here?


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!

<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