pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.2k stars 17.77k forks source link

pandas.Series.groupby example is not relevant #59309

Closed n49o7 closed 1 month ago

n49o7 commented 1 month ago

Hello,

On this page, in the first example, we can read ser.groupby(["a", "b", "a", "b"]).mean().

However, a and b are not part of the index at this point. The index only has Falcon and Parrot: pandas/core/series.py L1818.

Values a and b are relevant for the third example: pandas/core/series.py L1862.

asishm commented 1 month ago

The example you have referenced isn't grouping based on the index of the series, but is using the list to do the grouping. It would have the same result as ser.groupby([0,1,0,1]).mean() or ser.groupby([3,5,3,5]).mean()

Specifically from the docs:

If a list or ndarray of length equal to the selected axis is passed (see the groupby user guide), the values are used as-is to determine the groups.

MarcoGorelli commented 1 month ago

thanks @asishm

maybe an extra sentence before each snippet could help, e.g.

        We can pass a list of values to group by:

        >>> ser.groupby(["a", "b", "a", "b"]).mean()
        a    210.0
        b    185.0
        Name: Max Speed, dtype: float64

        We can group by a level of the index:

        >>> ser.groupby(level=0).mean()
        Falcon    370.0
        Parrot     25.0
        Name: Max Speed, dtype: float64

        We can pass a Series to group by:

        >>> ser.groupby(ser > 100).mean()
        Max Speed
        False     25.0
        True     370.0

would help make it clearer to users

ApoorvApoorv commented 1 month ago

Hi, I'd like to work on this issue. This would be my first time contributing to an open-source repository, and a well-renowned library like Pandas seems like a great place to start. Is this possible?

Thank you!

MarcoGorelli commented 1 month ago

Yes, you don't need to ask for permission

ApoorvApoorv commented 1 month ago

take

asishm commented 1 month ago

Agree with that @MarcoGorelli I'd also change using a list with a/bs to something else to avoid the confusion that @n49o7 faced.