modin.pandas.DataFrame.groupby

DataFrame.groupby(by=None, axis: Axis | NoDefault = _NoDefault.no_default, 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]

Group DataFrame 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 Snowpark pandas 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 SnowparkPandas. 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 DataFrameGroupBy

Examples:

>>> 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

**Hierarchical 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'))
>>> df = pd.DataFrame({'Max Speed': [390., 350., 30., 20.]},
...                   index=index)
>>> df      
                Max Speed
Animal Type
Falcon Captive      390.0
       Wild         350.0
Parrot Captive       30.0
       Wild          20.0

>>> df.groupby(level=0).mean()      
        Max Speed
Animal
Falcon      370.0
Parrot       25.0

>>> df.groupby(level="Type").mean()     
         Max Speed
Type
Captive      210.0
Wild         185.0
Copy