pandas.Series.groupby#

Series.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, observed=_NoDefault.no_default, dropna=True)[源代码]#

使用映射器或按列组成的 Series 对 Series 进行分组。

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.

Parameters:
bymapping, function, label, pd.Grouper or 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 或 ‘index’, 1 或 ‘columns’}, default 0

沿行(0)或列(1)分割。对于 Series,此参数未使用,默认值为 0。

自 2.1.0 版本弃用: 将在未来版本中移除并行为等同于 axis=0。对于 axis=1,请改用 frame.T.groupby(...)

levelint、级别名称或其序列,默认 None

如果轴是 MultiIndex(分层的),则按特定级别或级别进行分组。请勿同时指定 bylevel

as_indexbool, default True

返回一个以组标签作为索引的对象。仅对 DataFrame 输入相关。as_index=False 实际上是“SQL 风格”的分组输出。此参数对过滤(参见 filtrations in the user guide )没有影响,例如 head()tail()nth() 以及转换(参见 transformations in the user guide )。

sortbool, default True

对组键进行排序。通过关闭此项可获得更好的性能。注意,这不会影响每个组内观测值的顺序。GroupBy 会保留每个组内行的顺序。如果为 False,则组将按其在原始 DataFrame 中出现的顺序显示。此参数对过滤(参见 filtrations in the user guide )没有影响,例如 head()tail()nth() 以及转换(参见 transformations in the user guide )。

在 2.0.0 版本发生变更: 指定 sort=False 并使用有序的分类分组器将不再对值进行排序。

group_keysbool, default True

调用 apply 并且 by 参数产生一个具有类似索引(即 a transform )的结果时,将组键添加到索引中以标识各个部分。默认情况下,当结果的索引(和列)标签与输入匹配时,不包含组键,否则会包含。

在 1.5.0 版本发生变更: 警告:当 apply 的结果是类似索引的 Series 或 DataFrame 时,group_keys 将不再被忽略。显式指定 group_keys 以包含或不包含组键。

在 2.0.0 版本发生变更: group_keys 现在默认值为 True

observedbool,默认 False

这仅在任何分组器是 Categorical 时才适用。如果为 True:仅为分类分组器显示观察到的值。如果为 False:为分类分组器显示所有值。

自 2.1.0 版本弃用: 在 pandas 的未来版本中,默认值将更改为 True。

dropnabool, default True

如果为 True,并且组键包含 NA 值,则 NA 值将与行/列一起被丢弃。如果为 False,则 NA 值也将被视为组中的键。

Returns:
pandas.api.typing.SeriesGroupBy

返回一个包含组信息的 Groupby 对象。

参见

resample

用于时间序列的频率转换和重采样的便利方法。

Notes

请参阅 user guide 以获取更详细的用法和示例,包括分割对象为组、遍历组、选择组、聚合等。

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(["a", "b", "a", "b"]).mean()
a    210.0
b    185.0
Name: Max Speed, dtype: float64
>>> ser.groupby(level=0).mean()
Falcon    370.0
Parrot     25.0
Name: Max Speed, dtype: float64
>>> ser.groupby(ser > 100).mean()
Max Speed
False     25.0
True     370.0
Name: Max Speed, dtype: float64

按索引分组

我们可以使用 level 参数按分层索引的不同级别进行分组:

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

我们还可以通过定义 dropna 参数来选择是否包含 NA 作为组键,默认设置为 True

>>> ser = pd.Series([1, 2, 3, 3], index=["a", 'a', 'b', np.nan])
>>> ser.groupby(level=0).sum()
a    3
b    3
dtype: int64
>>> ser.groupby(level=0, dropna=False).sum()
a    3
b    3
NaN  3
dtype: int64
>>> 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
>>> ser.groupby(["a", "b", "a", np.nan], dropna=False).mean()
a    210.0
b    350.0
NaN   20.0
Name: Max Speed, dtype: float64