pandas.DataFrame.groupby#

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

使用映射器或列Series对DataFrame进行分组。

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

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

参见

resample

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

Notes

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

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

分层索引

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

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

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

>>> l = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]]
>>> df = pd.DataFrame(l, columns=["a", "b", "c"])
>>> df.groupby(by=["b"]).sum()
    a   c
b
1.0 2   3
2.0 2   5
>>> df.groupby(by=["b"], dropna=False).sum()
    a   c
b
1.0 2   3
2.0 2   5
NaN 1   4
>>> l = [["a", 12, 12], [None, 12.3, 33.], ["b", 12.3, 123], ["a", 1, 1]]
>>> df = pd.DataFrame(l, columns=["a", "b", "c"])
>>> df.groupby(by="a").sum()
    b     c
a
a   13.0   13.0
b   12.3  123.0
>>> df.groupby(by="a", dropna=False).sum()
    b     c
a
a   13.0   13.0
b   12.3  123.0
NaN 12.3   33.0

使用 .apply() 时,使用 group_keys 来包含或排除组键。 group_keys 参数默认为 ``True``(包含)。

>>> df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
...                               'Parrot', 'Parrot'],
...                    'Max Speed': [380., 370., 24., 26.]})
>>> df.groupby("Animal", group_keys=True)[['Max Speed']].apply(lambda x: x)
          Max Speed
Animal
Falcon 0      380.0
       1      370.0
Parrot 2       24.0
       3       26.0
>>> df.groupby("Animal", group_keys=False)[['Max Speed']].apply(lambda x: x)
   Max Speed
0      380.0
1      370.0
2       24.0
3       26.0