Skip to content

DOC : Updated the comparison with spreadsheets with GroupBy #39965

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 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
Binary file added doc/source/_static/spreadsheets/group-by.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,47 @@ In Excel, there are `merging of tables can be done through a VLOOKUP
* It will include all columns from the lookup table, instead of just a single specified column
* It supports :ref:`more complex join operations <merging.join>`

GroupBy
--------------------

In Excel, this can be done by using the Query Editor.
You can group the values in various rows into a single value by grouping the rows according to the values in one or more columns
Then using the Query Editor ribbon, Right-click the column header that you want to group on,
and click `Group By <https://support.microsoft.com/en-us/office/group-rows-in-a-table-power-query-e1b9e916-6fcc-40bf-a6e8-ef928240adf1>`_.
Copy link
Member

Choose a reason for hiding this comment

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

I could see Excel changing where to find the Query Editor, so let's try and future-proof this documentation by not getting into those details, directing readers to the canonical Excel documentation instead.


Power Query has two types of Group By operations:

* Aggregate a column with an aggregate function
* Perform a row operation

To aggregate a column, select the column to perform the Aggregate Operation on from the Column drop-down.
A Row Operation does not require a Column, since data is grouped based on table rows.

.. image:: ../../_static/spreadsheets/group-by.png
Copy link
Member

Choose a reason for hiding this comment

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

Right now, this section duplicates content from the canonical documentation, which doesn't add any value. I suggest we either:

  • Walk them through an example in Excel that matches the pandas example below, so we are comparing apples to apples
  • Cut from "Then using the…" through this image

Copy link
Member

Choose a reason for hiding this comment

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

@afeld - are you good here?

Copy link
Member

Choose a reason for hiding this comment

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

Reviewing this again, I see that the requested change was not made. I also agree that having an excel example that matches the pandas example would be desirable.

Copy link
Member

Choose a reason for hiding this comment

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

@Bhard27 Are you able to update the excel image to match the pandas example below?


The equivalent in pandas:
We can use the `Group By operation <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html>`_ .
For example -

.. ipython:: python

df = pd.DataFrame(
[
("bird", "Falconiformes", 389.0),
("bird", "Psittaciformes", 24.0),
("mammal", "Carnivora", 80.2),
("mammal", "Primates", np.nan),
("mammal", "Carnivora", 58),
],
index=["falcon", "parrot", "lion", "monkey", "leopard"],
columns=("class", "order", "max_speed"),
)
Copy link
Member

Choose a reason for hiding this comment

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

We have an outstanding task in #38990 around standardizing the example datasets used in the page. Totally fine to take care of that as follow-up; just noting.

df

# default is axis=0
grouped = df.groupby("class")
grouped = df.groupby("order", axis="columns")
grouped = df.groupby(["class", "order"])
Copy link
Member

Choose a reason for hiding this comment

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

Assigning to a variable doesn't output anything, and even if it did, it would be something not useful like

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7f8424dc3d90>

Let's do groupby().mean() to show something more interesting.


Other considerations
--------------------
Expand Down