Skip to content

DOC: add notes to the groupby.rst docs #7004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 47 additions & 2 deletions doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,9 @@ Aggregation
-----------

Once the GroupBy object has been created, several methods are available to
perform a computation on the grouped data. An obvious one is aggregation via
the ``aggregate`` or equivalently ``agg`` method:
perform a computation on the grouped data.

An obvious one is aggregation via the ``aggregate`` or equivalently ``agg`` method:

.. ipython:: python

Expand Down Expand Up @@ -382,6 +383,22 @@ index are the group names and whose values are the sizes of each group.

grouped.size()

.. ipython:: python

grouped.describe()

.. note::

Aggregation functions will **not** return the groups that you are aggregating over
if they are named *columns*, when ``as_index=True``, the default. The grouped columns will
be the **indices** of the returned object.

Aggregating functions are ones that reduce the dimension of the returned objects,
for example: ``mean, sum, size, count, std, var, describe, first, last, min, max``. This is
very much like performing a redcing operation on a ``DataFrame`` and getting a ``Series`` back.
Copy link
Member

Choose a reason for hiding this comment

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

unless you specify as_index=False?


Passing ``as_index=False`` **will** return the groups that you are aggregative over if they are
named *columns*.

.. _groupby.aggregate.multifunc:

Expand Down Expand Up @@ -537,6 +554,16 @@ and that the transformed data contains no NAs.
grouped_trans.count() # counts after transformation
grouped_trans.size() # Verify non-NA count equals group size

.. note::

Some functions when applied to a groupby object will automatically transform the input, returning
an object of the same shape as the original. For example: ``fillna, ffill, bfill, shift``.
Passing ``as_index=False`` will not affect these transformation methods.

.. ipython:: python

grouped.ffill()

.. _groupby.filter:

Filtration
Expand Down Expand Up @@ -579,6 +606,18 @@ For dataframes with multiple columns, filters should explicitly specify a column
dff['C'] = np.arange(8)
dff.groupby('B').filter(lambda x: len(x['C']) > 2)

.. note::

Some functions when applied to a groupby object will act as a **filter** on the input, returning
a reduced shape of the original (and potentitally eliminating groups), but with the index unchanged.
Passing ``as_index=False`` will not affect these transformation methods.
For example: ``head, tail nth``.

.. ipython:: python

dff.groupby('B').head(2)


.. _groupby.dispatch:

Dispatching to instance methods
Expand Down Expand Up @@ -664,6 +703,12 @@ The dimension of the returned result can also change:
s.apply(f)


.. note::

``apply`` can act as a reducer, transformer, *or* filter function, depending on exactly what is passed to apply.
So depending on the path taken, and exactly what you are grouping. Thus the grouped columns(s) may be included in
the output as well as set the indices.

.. warning::

In the current implementation apply calls func twice on the
Expand Down