grouped = s.groupby(s)

Or:

grouped = s.groupby(lambda x: s[x])
Answer from user1201614 on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.2 documentation
We can pass a list of values to group the Series data by custom labels: >>> ser.groupby(["a", "b", "a", "b"]).mean() a 210.0 b 185.0 Name: Max Speed, dtype: float64 · Grouping by numeric labels yields similar results: >>> ser.groupby([0, 1, 0, 1]).mean() 0 210.0 1 185.0 Name: Max Speed, dtype: float64 ... >>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'], ... ['Captive', 'Wild', 'Captive', 'Wild']] >>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) >>> ser = pd.Series([390., 350., 30., 20.], index=index, name="Max Speed") >>> ser Animal Type Falcon Captive 390.0 Wild 350.0 Parrot Captive 30.0 Wild 20.0 Name: Max Speed, dtype: float64
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.1 documentation
We can pass a list of values to group the Series data by custom labels: >>> ser.groupby(["a", "b", "a", "b"]).mean() a 210.0 b 185.0 Name: Max Speed, dtype: float64 · Grouping by numeric labels yields similar results: >>> ser.groupby([0, 1, 0, 1]).mean() 0 210.0 1 185.0 Name: Max Speed, dtype: float64 ... >>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'], ... ['Captive', 'Wild', 'Captive', 'Wild']] >>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) >>> ser = pd.Series([390., 350., 30., 20.], index=index, name="Max Speed") >>> ser Animal Type Falcon Captive 390.0 Wild 350.0 Parrot Captive 30.0 Wild 20.0 Name: Max Speed, dtype: float64
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.indices.html
pandas.core.groupby.SeriesGroupBy.indices — pandas 2.3.3 documentation
index=["owl", "toucan", "eagle"]) >>> df a b c owl 1 2 3 toucan 1 5 6 eagle 7 8 9 >>> df.groupby(by=["a"]).indices {1: array([0, 1]), 7: array([2])} For Resampler: >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › how to groupby index in pandas?
How to GroupBy Index in Pandas? - Spark By {Examples}
July 3, 2025 - How to perform groupby index in pandas? Pass the index name of the DataFrame as a parameter to the groupby() function to group rows on an index.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 1.5.2 documentation
Returns a groupby object that contains information about the groups. ... Convenience method for frequency conversion and resampling of time series. ... See the user guide for more detailed usage and examples, including splitting an object into groups, iterating through groups, selecting a group, ...
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas ain documentation
We can pass a list of values to group the Series data by custom labels: >>> ser.groupby(["a", "b", "a", "b"]).mean() a 210.0 b 185.0 Name: Max Speed, dtype: float64 · Grouping by numeric labels yields similar results: >>> ser.groupby([0, 1, 0, 1]).mean() 0 210.0 1 185.0 Name: Max Speed, dtype: float64 ... >>> arrays = [ ... ["Falcon", "Falcon", "Parrot", "Parrot"], ... ["Captive", "Wild", "Captive", "Wild"], ... ] >>> index = pd.MultiIndex.from_arrays(arrays, names=("Animal", "Type")) >>> ser = pd.Series([390.0, 350.0, 30.0, 20.0], index=index, name="Max Speed") >>> ser Animal Type Falcon Captive 390.0 Wild 350.0 Parrot Captive 30.0 Wild 20.0 Name: Max Speed, dtype: float64
🌐
w3resource
w3resource.com › pandas › series › series-groupby.php
Pandas: Series - groupby() function - w3resource
import numpy as np import pandas as pd arrays = [['Tiger', 'Tiger', 'Dog', 'Dog'], ['Captive', 'Wild', 'Captive', 'Wild']] index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) df = pd.DataFrame({'Max Speed': [280., 250., 30., 20.]}, index=index) df.groupby(level=1).mean() Output: Max Speed Type Captive 155.0 Wild 135.0 · Previous: Map values of Pandas Series Next: Rolling window calculations in Pandas  ·
🌐
Real Python
realpython.com › pandas-groupby
pandas GroupBy: Your Guide to Grouping Data in Python – Real Python
January 19, 2025 - Earlier you saw that the first parameter to .groupby() can accept several different arguments: ... You can take advantage of the last option in order to group by the day of the week. Use the index’s .day_name() to produce a pandas Index of strings.
Find elsewhere
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.22 › generated › pandas.Series.groupby.html
pandas.Series.groupby — pandas 0.22.0 documentation
Group series using mapper (dict or key function, apply given function to group, return result as series) or by a series of columns. Examples · DataFrame results · >>> data.groupby(func, axis=0).mean() >>> data.groupby(['col1', 'col2'])['col3'].mean() DataFrame with hierarchical index · >>> data.groupby(['col1', 'col2']).mean() index · modules | next | previous | pandas 0.22.0 documentation » ·
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.3 › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 0.25.3 documentation
Convenience method for frequency conversion and resampling of time series. ... See the user guide for more. ... >>> df = pd.DataFrame({'Animal': ['Falcon', 'Falcon', ... 'Parrot', 'Parrot'], ... 'Max Speed': [380., 370., 24., 26.]}) >>> df Animal Max Speed 0 Falcon 380.0 1 Falcon 370.0 2 Parrot 24.0 3 Parrot 26.0 >>> df.groupby(['Animal']).mean() Max Speed Animal Falcon 375.0 Parrot 25.0 ... >>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'], ... ['Captive', 'Wild', 'Captive', 'Wild']] >>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) >>> df = pd.DataFrame({'Max Speed': [390., 350., 30., 20.]}, ...
🌐
Pandas
pandas.pydata.org › docs › reference › groupby.html
GroupBy — pandas 3.0.2 documentation - PyData |
pandas.api.typing.DataFrameGroupBy and pandas.api.typing.SeriesGroupBy instances are returned by groupby calls pandas.DataFrame.groupby() and pandas.Series.groupby() respectively · DataFrameGroupBy.__iter__()
🌐
Statology
statology.org › home › pandas: how to group by index and perform calculation
Pandas: How to Group By Index and Perform Calculation
December 18, 2023 - You can use the following methods to group by one or more index columns in pandas and perform some calculation: Method 1: Group By One Index Column · df.groupby('index1')['numeric_column'].max() Method 2: Group By Multiple Index Columns · ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.aggregate.html
pandas.core.groupby.SeriesGroupBy.aggregate — pandas 2.3.3 documentation
Transforms the Series on each group based on the given function. ... Aggregate using one or more operations over the specified axis. ... When using engine='numba', there will be no “fall back” behavior internally. The group data and group index will be passed as numpy arrays to the JITed ...
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 3.0.0.dev0+2709.g415830fc8f documentation
We can pass a list of values to group the Series data by custom labels: >>> ser.groupby(["a", "b", "a", "b"]).mean() a 210.0 b 185.0 Name: Max Speed, dtype: float64 · Grouping by numeric labels yields similar results: >>> ser.groupby([0, 1, 0, 1]).mean() 0 210.0 1 185.0 Name: Max Speed, dtype: float64 ... >>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'], ... ['Captive', 'Wild', 'Captive', 'Wild']] >>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type')) >>> ser = pd.Series([390., 350., 30., 20.], index=index, name="Max Speed") >>> ser Animal Type Falcon Captive 390.0 Wild 350.0 Parrot Captive 30.0 Wild 20.0 Name: Max Speed, dtype: float64
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.core.groupby.SeriesGroupBy.count.html
pandas.core.groupby.SeriesGroupBy.count — pandas 2.3.3 documentation
>>> data = [[1, np.nan, 3], [1, np.nan, 6], [7, 8, 9]] >>> df = pd.DataFrame(data, columns=["a", "b", "c"], ... index=["cow", "horse", "bull"]) >>> df a b c cow 1 NaN 3 horse 1 NaN 6 bull 7 8.0 9 >>> df.groupby("a").count() b c a 1 0 2 7 1 1 ... >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.groupby.html
pandas.DataFrame.groupby — pandas 3.0.2 documentation
DataFrame.groupby(by=None, level=None, *, as_index=True, sort=True, group_keys=True, observed=True, dropna=True)[source]# Group DataFrame using a mapper or by a Series of columns.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › reference › api › pandas.Series.groupby.html
pandas.Series.groupby — pandas 2.1.4 documentation
Returns a groupby object that contains information about the groups. ... Convenience method for frequency conversion and resampling of time series. ... See the user guide for more detailed usage and examples, including splitting an object into groups, iterating through groups, selecting a group, ...