pandas 1.4.2

NotesParametersReturnsBackRef
groupby(self, by=None, axis=0, level=None, as_index: 'bool' = True, sort: 'bool' = True, group_keys: 'bool' = True, squeeze: 'bool | lib.NoDefault' = <no_default>, observed: 'bool' = False, dropna: 'bool' = True) -> 'SeriesGroupBy'

A groupby operation involves some combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups.

Notes

See the :None:None:`user guide <https://pandas.pydata.org/pandas-docs/stable/groupby.html>` for more detailed usage and examples, including splitting an object into groups, iterating through groups, selecting a group, aggregation, and more.

Parameters

by : mapping, function, label, or list of labels

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 :None:None:`groupby user guide <https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#splitting-an-object-into-groups>`), 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).

level : int, level name, or sequence of such, default None

If the axis is a MultiIndex (hierarchical), group by a particular level or levels.

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. Get better performance by turning this off. Note this does not influence the order of observations within each group. Groupby preserves the order of rows within each group.

group_keys : bool, default True

When calling apply, add group keys to index to identify pieces.

squeeze : bool, default False

Reduce the dimensionality of the return type if possible, otherwise return a consistent type.

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

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.

versionadded

Returns

SeriesGroupBy

Returns a groupby object that contains information about the groups.

Group Series using a mapper or by a Series of columns.

See Also

resample

Convenience method for frequency conversion and resampling of time series.

Examples

This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> ser.groupby(["a", "b", "a", "b"]).mean()
a    210.0
b    185.0
Name: Max Speed, dtype: float64
This example is valid syntax, but we were not able to check execution
>>> ser.groupby(level=0).mean()
Falcon    370.0
Parrot     25.0
Name: Max Speed, dtype: float64
This example is valid syntax, but we were not able to check execution
>>> ser.groupby(ser > 100).mean()
Max Speed
False     25.0
True     370.0
Name: Max Speed, dtype: float64

Grouping by Indexes

We can groupby different levels of a hierarchical index using the :None:None:`level` parameter:

This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> ser.groupby(level=0).mean()
Animal
Falcon    370.0
Parrot     25.0
Name: Max Speed, dtype: float64
This example is valid syntax, but we were not able to check execution
>>> ser.groupby(level="Type").mean()
Type
Captive    210.0
Wild       185.0
Name: Max Speed, dtype: float64

We can also choose to include :None:None:`NA` in group keys or not by defining dropna parameter, the default setting is :None:None:`True`.

This example is valid syntax, but we were not able to check execution
>>> ser = pd.Series([1, 2, 3, 3], index=["a", 'a', 'b', np.nan])
... ser.groupby(level=0).sum() a 3 b 3 dtype: int64
This example is valid syntax, but we were not able to check execution
>>> ser.groupby(level=0, dropna=False).sum()
a    3
b    3
NaN  3
dtype: int64
This example is valid syntax, but we were not able to check execution
>>> arrays = ['Falcon', 'Falcon', 'Parrot', 'Parrot']
... ser = pd.Series([390., 350., 30., 20.], index=arrays, name="Max Speed")
... ser.groupby(["a", "b", "a", np.nan]).mean() a 210.0 b 350.0 Name: Max Speed, dtype: float64
This example is valid syntax, but we were not able to check execution
>>> ser.groupby(["a", "b", "a", np.nan], dropna=False).mean()
a    210.0
b    350.0
NaN   20.0
Name: Max Speed, dtype: float64
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.groupby.groupby.GroupBy.mean pandas.core.groupby.groupby.GroupBy.ewm pandas.core.groupby.groupby.GroupBy.rolling pandas.core.groupby.groupby.GroupBy.any pandas.core.groupby.groupby.GroupBy.nth pandas.core.groupby.groupby.GroupBy.cummax pandas.core.groupby.groupby.GroupBy.std pandas.core.groupby.groupby.GroupBy.size pandas.core.groupby.groupby.GroupBy.all pandas.core.groupby.groupby.GroupBy.expanding pandas.core.groupby.groupby.GroupBy.cumsum pandas.core.groupby.groupby.GroupBy.tail pandas.core.groupby.groupby.GroupBy.ohlc pandas.core.groupby.groupby.GroupBy.cummin pandas.core.groupby.groupby.GroupBy.var pandas.core.groupby.groupby.GroupBy.sem pandas.core.groupby.groupby.GroupBy.cumprod pandas.core.groupby.groupby.GroupBy.median pandas.core.groupby.groupby.GroupBy.pct_change pandas.core.groupby.groupby.GroupBy.head pandas.core.groupby.groupby.GroupBy.count pandas.core.groupby.groupby.GroupBy.rank

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/series.py#1807
type: <class 'function'>
Commit: