modin.pandas.Series.groupby¶
- Series.groupby(by=None, axis: Axis = 0, level: IndexLabel | None = None, as_index: bool = True, sort: bool = True, group_keys: bool = True, observed: bool | NoDefault = _NoDefault.no_default, dropna: bool = True)[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.26.0/snowpark-python/src/snowflake/snowpark/modin/plugin/extensions/series_overrides.py#L1378-L1416)¶
Group Series using a mapper or by a Series of columns.
- Parameters:
by – mapping, function, label, Snowpark pandas Series or a list of such. Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s 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), 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. Notice that a tuple is interpreted as a (single) key.
axis – {0 or ‘index’, 1 or ‘columns’}, default 0 Split along rows (0) or columns (1). For Series this parameter is unused and defaults to 0.
level – int, level name, or sequence of such, default None If the axis is a MultiIndex (hierarchical), group by a particular level or levels. Do not specify both by and level.
as_index – bool, default True For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output.
sort – bool, default True Sort group keys. Groupby preserves the order of rows within each group. Note that in pandas, better performance can be achieved by turning sort off, this is not going to be true with Snowpark pandas API. When sort=False, the performance will be no better than sort=True.
group_keys – bool, default True When calling apply and the by argument produces a like-indexed (i.e. a transform) result, add group keys to index to identify pieces. By default, group keys are not included when the result’s index (and column) labels match the inputs, and are included otherwise.
observed – bool, default False This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers. This parameter is currently ignored with Snowpark pandas API, since Category type is currently not supported with Snowpark pandas API.
dropna – bool, default True If True, and if group keys contain NA values, NA values together with row/column will be dropped. If False, NA values will also be treated as the key in groups.
- Returns:
Returns a groupby object that contains information about the groups.
- Return type:
Snowpark pandas SeriesGroupBy
- Examples::
>>> ser = pd.Series([390., 350., 30., 20.], ... index=['Falcon', 'Falcon', 'Parrot', 'Parrot'], ... name="Max Speed") >>> ser Falcon 390.0 Falcon 350.0 Parrot 30.0 Parrot 20.0 Name: Max Speed, dtype: float64 >>> ser.groupby(level=0).mean() Falcon 370.0 Parrot 25.0 Name: Max Speed, dtype: float64
Grouping by Indexes
We can groupby different levels of a hierarchical index using the level parameter:
>>> 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 >>> ser.groupby(level=0).mean() Animal Falcon 370.0 Parrot 25.0 Name: Max Speed, dtype: float64 >>> ser.groupby(level="Type").mean() Type Captive 210.0 Wild 185.0 Name: Max Speed, dtype: float64