Pandas 中文参考指南

Group by: split-apply-combine

我们指的“按组分配”是一个涉及以下一个或多个步骤的过程:

By “group by” we are referring to a process involving one or more of the following steps:

  1. Splitting the data into groups based on some criteria.

  2. Applying a function to each group independently.

  3. Combining the results into a data structure.

在这些情况中,拆分步骤是最直接的。在应用步骤中,我们可能希望执行以下操作之一:

Out of these, the split step is the most straightforward. In the apply step, we might wish to do one of the following:

  1. Aggregation: compute a summary statistic (or statistics) for each group. Some examples:

  2. Compute group sums or means.

  3. Compute group sizes / counts.

  4. Transformation: perform some group-specific computations and return a like-indexed object. Some examples:

  5. Standardize data (zscore) within a group.

  6. Filling NAs within groups with a value derived from each group.

  7. Filtration: discard some groups, according to a group-wise computation that evaluates to True or False. Some examples:

  8. Discard data that belong to groups with only a few members.

  9. Filter out data based on the group sum or mean.

许多这些运算符都是在 GroupBy 对象上定义的。这些运算符与 aggregating APIwindow APIresample API 类似。

Many of these operations are defined on GroupBy objects. These operations are similar to those of the aggregating API, window API, and resample API.

给定运算符可能不属于这些类别中的一类,也可能是它们的某种组合。在这种情况下,可能可以通过 GroupBy 的 apply 方法来计算运算符。此方法将检查 apply 步骤的结果,并尝试将其合理地组合到单个结果中(如果它不属于上述三类中的任何一类)。

It is possible that a given operation does not fall into one of these categories or is some combination of them. In such a case, it may be possible to compute the operation using GroupBy’s apply method. This method will examine the results of the apply step and try to sensibly combine them into a single result if it doesn’t fit into either of the above three categories.

使用内置 GroupBy 运算符分成多个步骤的运算符比使用 apply 方法和用户定义的 Python 函数效率更高。

An operation that is split into multiple steps using built-in GroupBy operations will be more efficient than using the apply method with a user-defined Python function.

对于使用过基于 SQL 的工具(或 itertools)的用户而言,GroupBy 名称应该非常熟悉,您可以在其中编写如下代码:

The name GroupBy should be quite familiar to those who have used a SQL-based tool (or itertools), in which you can write code like:

SELECT Column1, Column2, mean(Column3), sum(Column4)
FROM SomeTable
GROUP BY Column1, Column2

我们旨在使用 pandas 使这样的操作自然且易于表达。我们将介绍 GroupBy 功能的各个方面,然后提供一些非平凡的示例/用例。

We aim to make operations like this natural and easy to express using pandas. We’ll address each area of GroupBy functionality, then provide some non-trivial examples / use cases.

有关一些高级策略,请参见 cookbook

See the cookbook for some advanced strategies.

Splitting an object into groups

分组的抽象定义是提供标签到组名的映射。要创建一个 GroupBy 对象(有关 GroupBy 对象的更多信息,请参见后面),您可以执行以下操作:

The abstract definition of grouping is to provide a mapping of labels to group names. To create a GroupBy object (more on what the GroupBy object is later), you may do the following:

In [1]: speeds = pd.DataFrame(
   ...:     [
   ...:         ("bird", "Falconiformes", 389.0),
   ...:         ("bird", "Psittaciformes", 24.0),
   ...:         ("mammal", "Carnivora", 80.2),
   ...:         ("mammal", "Primates", np.nan),
   ...:         ("mammal", "Carnivora", 58),
   ...:     ],
   ...:     index=["falcon", "parrot", "lion", "monkey", "leopard"],
   ...:     columns=("class", "order", "max_speed"),
   ...: )
   ...:

In [2]: speeds
Out[2]:
          class           order  max_speed
falcon     bird   Falconiformes      389.0
parrot     bird  Psittaciformes       24.0
lion     mammal       Carnivora       80.2
monkey   mammal        Primates        NaN
leopard  mammal       Carnivora       58.0

In [3]: grouped = speeds.groupby("class")

In [4]: grouped = speeds.groupby(["class", "order"])

可以通过多种不同的方式指定映射:

The mapping can be specified many different ways:

  1. A Python function, to be called on each of the index labels.

  2. A list or NumPy array of the same length as the index.

  3. A dict or Series, providing a label group name mapping.

  4. For DataFrame objects, a string indicating either a column name or an index level name to be used to group.

  5. A list of any of the above things.

我们统称分组对象为键。例如,请考虑以下内容 DataFrame

Collectively we refer to the grouping objects as the keys. For example, consider the following DataFrame:

传递给 groupby 的字符串可以引用列或索引层。如果字符串同时匹配列名和索引层名,则会引发 ValueError

A string passed to groupby may refer to either a column or an index level. If a string matches both a column name and an index level name, a ValueError will be raised.

In [5]: df = pd.DataFrame(
   ...:     {
   ...:         "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
   ...:         "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
   ...:         "C": np.random.randn(8),
   ...:         "D": np.random.randn(8),
   ...:     }
   ...: )
   ...:

In [6]: df
Out[6]:
     A      B         C         D
0  foo    one  0.469112 -0.861849
1  bar    one -0.282863 -2.104569
2  foo    two -1.509059 -0.494929
3  bar  three -1.135632  1.071804
4  foo    two  1.212112  0.721555
5  bar    two -0.173215 -0.706771
6  foo    one  0.119209 -1.039575
7  foo  three -1.044236  0.271860

在 DataFrame 上,我们通过调用 groupby() 来获取 GroupBy 对象。此方法返回一个 pandas.api.typing.DataFrameGroupBy 实例。我们当然可以通过 AB 列(或两者)进行分组:

On a DataFrame, we obtain a GroupBy object by calling groupby(). This method returns a pandas.api.typing.DataFrameGroupBy instance. We could naturally group by either the A or B columns, or both:

In [7]: grouped = df.groupby("A")

In [8]: grouped = df.groupby("B")

In [9]: grouped = df.groupby(["A", "B"])

df.groupby('A') 只是 df.groupby(df['A']) 的语法糖。

df.groupby('A') is just syntactic sugar for df.groupby(df['A']).

如果我们还对列 AB 使用 MultiIndex,则我们可以对除我们指定的列之外的所有列进行分组:

If we also have a MultiIndex on columns A and B, we can group by all the columns except the one we specify:

In [10]: df2 = df.set_index(["A", "B"])

In [11]: grouped = df2.groupby(level=df2.index.names.difference(["B"]))

In [12]: grouped.sum()
Out[12]:
            C         D
A
bar -1.591710 -1.739537
foo -0.752861 -1.402938

上述 GroupBy 将根据其索引(行)来划分 DataFrame。要根据列划分,首先进行转置:

The above GroupBy will split the DataFrame on its index (rows). To split by columns, first do a transpose:

In [13]: def get_letter_type(letter):
   ....:     if letter.lower() in 'aeiou':
   ....:         return 'vowel'
   ....:     else:
   ....:         return 'consonant'
   ....:

In [14]: grouped = df.T.groupby(get_letter_type)

pandas Index 对象支持重复值。如果在 groupby 操作中将非唯一索引用作组键,则具有相同索引值的所有值都将被视为属于一个组,因此聚合函数的输出将仅包含唯一索引值:

pandas Index objects support duplicate values. If a non-unique index is used as the group key in a groupby operation, all values for the same index value will be considered to be in one group and thus the output of aggregation functions will only contain unique index values:

In [15]: index = [1, 2, 3, 1, 2, 3]

In [16]: s = pd.Series([1, 2, 3, 10, 20, 30], index=index)

In [17]: s
Out[17]:
1     1
2     2
3     3
1    10
2    20
3    30
dtype: int64

In [18]: grouped = s.groupby(level=0)

In [19]: grouped.first()
Out[19]:
1    1
2    2
3    3
dtype: int64

In [20]: grouped.last()
Out[20]:
1    10
2    20
3    30
dtype: int64

In [21]: grouped.sum()
Out[21]:
1    11
2    22
3    33
dtype: int64

请注意,在需要之前不会发生拆分。创建 GroupBy 对象仅验证您是否传递了有效的映射。

Note that no splitting occurs until it’s needed. Creating the GroupBy object only verifies that you’ve passed a valid mapping.

许多类型的复杂数据处理可以用 GroupBy 操作来表示(尽管不能保证是最有效的实现)。您可以对标签映射函数进行非常有创意的操作。

Many kinds of complicated data manipulations can be expressed in terms of GroupBy operations (though it can’t be guaranteed to be the most efficient implementation). You can get quite creative with the label mapping functions.

GroupBy sorting

默认情况下,在 groupby 操作期间对组键进行排序。但您可以传递 sort=False 以潜在加快速度。通过 sort=False,组键之间的顺序遵循键在原始数据框中出现的顺序:

By default the group keys are sorted during the groupby operation. You may however pass sort=False for potential speedups. With sort=False the order among group-keys follows the order of appearance of the keys in the original dataframe:

In [22]: df2 = pd.DataFrame({"X": ["B", "B", "A", "A"], "Y": [1, 2, 3, 4]})

In [23]: df2.groupby(["X"]).sum()
Out[23]:
   Y
X
A  7
B  3

In [24]: df2.groupby(["X"], sort=False).sum()
Out[24]:
   Y
X
B  3
A  7

请注意,groupby 将保留每个组中观察结果的排序顺序。例如,下面 groupby() 创建的组按它们在原始 DataFrame 中出现的顺序排序:

Note that groupby will preserve the order in which observations are sorted within each group. For example, the groups created by groupby() below are in the order they appeared in the original DataFrame:

In [25]: df3 = pd.DataFrame({"X": ["A", "B", "A", "B"], "Y": [1, 4, 3, 2]})

In [26]: df3.groupby("X").get_group("A")
Out[26]:
   X  Y
0  A  1
2  A  3

In [27]: df3.groupby(["X"]).get_group(("B",))
Out[27]:
   X  Y
1  B  4
3  B  2

默认情况下,在 groupby 操作期间将 NA 值从组键中排除。但是,如果您想在组键中包括 NA 值,则可以传递 dropna=False 以实现此目的。

By default NA values are excluded from group keys during the groupby operation. However, in case you want to include NA values in group keys, you could pass dropna=False to achieve it.

In [28]: df_list = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]]

In [29]: df_dropna = pd.DataFrame(df_list, columns=["a", "b", "c"])

In [30]: df_dropna
Out[30]:
   a    b  c
0  1  2.0  3
1  1  NaN  4
2  2  1.0  3
3  1  2.0  2
# Default ``dropna`` is set to True, which will exclude NaNs in keys
In [31]: df_dropna.groupby(by=["b"], dropna=True).sum()
Out[31]:
     a  c
b
1.0  2  3
2.0  2  5

# In order to allow NaN in keys, set ``dropna`` to False
In [32]: df_dropna.groupby(by=["b"], dropna=False).sum()
Out[32]:
     a  c
b
1.0  2  3
2.0  2  5
NaN  1  4

dropna 参数的默认设置是 True,这意味着 NA 不包含在组键中。

The default setting of dropna argument is True which means NA are not included in group keys.

GroupBy object attributes

groups 属性是一个字典,其键是计算所得的唯一组,相应的值是属于每个组的轴标签。在上面的示例中,我们有:

The groups attribute is a dictionary whose keys are the computed unique groups and corresponding values are the axis labels belonging to each group. In the above example we have:

In [33]: df.groupby("A").groups
Out[33]: {'bar': [1, 3, 5], 'foo': [0, 2, 4, 6, 7]}

In [34]: df.T.groupby(get_letter_type).groups
Out[34]: {'consonant': ['B', 'C', 'D'], 'vowel': ['A']}

在 GroupBy 对象上调用标准 Python len 函数将返回组的数量,这与 groups 字典的长度相同:

Calling the standard Python len function on the GroupBy object returns the number of groups, which is the same as the length of the groups dictionary:

In [35]: grouped = df.groupby(["A", "B"])

In [36]: grouped.groups
Out[36]: {('bar', 'one'): [1], ('bar', 'three'): [3], ('bar', 'two'): [5], ('foo', 'one'): [0, 6], ('foo', 'three'): [7], ('foo', 'two'): [2, 4]}

In [37]: len(grouped)
Out[37]: 6

GroupBy 将制表符自动完成列名、GroupBy 操作和其他属性:

GroupBy will tab complete column names, GroupBy operations, and other attributes:

In [38]: n = 10

In [39]: weight = np.random.normal(166, 20, size=n)

In [40]: height = np.random.normal(60, 10, size=n)

In [41]: time = pd.date_range("1/1/2000", periods=n)

In [42]: gender = np.random.choice(["male", "female"], size=n)

In [43]: df = pd.DataFrame(
   ....:     {"height": height, "weight": weight, "gender": gender}, index=time
   ....: )
   ....:

In [44]: df
Out[44]:
               height      weight  gender
2000-01-01  42.849980  157.500553    male
2000-01-02  49.607315  177.340407    male
2000-01-03  56.293531  171.524640    male
2000-01-04  48.421077  144.251986  female
2000-01-05  46.556882  152.526206    male
2000-01-06  68.448851  168.272968  female
2000-01-07  70.757698  136.431469    male
2000-01-08  58.909500  176.499753  female
2000-01-09  76.435631  174.094104  female
2000-01-10  45.306120  177.540920    male

In [45]: gb = df.groupby("gender")
In [46]: gb.<TAB>  # noqa: E225, E999
gb.agg        gb.boxplot    gb.cummin     gb.describe   gb.filter     gb.get_group  gb.height     gb.last       gb.median     gb.ngroups    gb.plot       gb.rank       gb.std        gb.transform
gb.aggregate  gb.count      gb.cumprod    gb.dtype      gb.first      gb.groups     gb.hist       gb.max        gb.min        gb.nth        gb.prod       gb.resample   gb.sum        gb.var
gb.apply      gb.cummax     gb.cumsum     gb.fillna     gb.gender     gb.head       gb.indices    gb.mean       gb.name       gb.ohlc       gb.quantile   gb.size       gb.tail       gb.weight

GroupBy with MultiIndex

使用 hierarchically-indexed data 时,按层次结构的一个级别分组是相当自然的。

With hierarchically-indexed data, it’s quite natural to group by one of the levels of the hierarchy.

让我们创建一个具有两级 MultiIndex 的 Series。

Let’s create a Series with a two-level MultiIndex.

In [47]: arrays = [
   ....:     ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
   ....:     ["one", "two", "one", "two", "one", "two", "one", "two"],
   ....: ]
   ....:

In [48]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"])

In [49]: s = pd.Series(np.random.randn(8), index=index)

In [50]: s
Out[50]:
first  second
bar    one      -0.919854
       two      -0.042379
baz    one       1.247642
       two      -0.009920
foo    one       0.290213
       two       0.495767
qux    one       0.362949
       two       1.548106
dtype: float64

然后我们可以在 s 中按其中一个级别分组。

We can then group by one of the levels in s.

In [51]: grouped = s.groupby(level=0)

In [52]: grouped.sum()
Out[52]:
first
bar   -0.962232
baz    1.237723
foo    0.785980
qux    1.911055
dtype: float64

如果 MultiIndex 指定了名称,则可以传递这些名称而不是级别编号:

If the MultiIndex has names specified, these can be passed instead of the level number:

In [53]: s.groupby(level="second").sum()
Out[53]:
second
one    0.980950
two    1.991575
dtype: float64

支持使用多个级别进行分组。

Grouping with multiple levels is supported.

In [54]: arrays = [
   ....:     ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
   ....:     ["doo", "doo", "bee", "bee", "bop", "bop", "bop", "bop"],
   ....:     ["one", "two", "one", "two", "one", "two", "one", "two"],
   ....: ]
   ....:

In [55]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second", "third"])

In [56]: s = pd.Series(np.random.randn(8), index=index)

In [57]: s
Out[57]:
first  second  third
bar    doo     one     -1.131345
               two     -0.089329
baz    bee     one      0.337863
               two     -0.945867
foo    bop     one     -0.932132
               two      1.956030
qux    bop     one      0.017587
               two     -0.016692
dtype: float64

In [58]: s.groupby(level=["first", "second"]).sum()
Out[58]:
first  second
bar    doo      -1.220674
baz    bee      -0.608004
foo    bop       1.023898
qux    bop       0.000895
dtype: float64

可以将索引级别名称提供为键。

Index level names may be supplied as keys.

In [59]: s.groupby(["first", "second"]).sum()
Out[59]:
first  second
bar    doo      -1.220674
baz    bee      -0.608004
foo    bop       1.023898
qux    bop       0.000895
dtype: float64

有关 sum 函数和聚合的更多信息请参阅后面的内容。

More on the sum function and aggregation later.

Grouping DataFrame with Index levels and columns

一个 DataFrame 可以按列和索引级别的组合进行分组。您可以同时指定列和索引名称,或使用 Grouper

A DataFrame may be grouped by a combination of columns and index levels. You can specify both column and index names, or use a Grouper.

让我们首先创建一个具有 MultiIndex 的 DataFrame:

Let’s first create a DataFrame with a MultiIndex:

In [60]: arrays = [
   ....:     ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
   ....:     ["one", "two", "one", "two", "one", "two", "one", "two"],
   ....: ]
   ....:

In [61]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"])

In [62]: df = pd.DataFrame({"A": [1, 1, 1, 1, 2, 2, 3, 3], "B": np.arange(8)}, index=index)

In [63]: df
Out[63]:
              A  B
first second
bar   one     1  0
      two     1  1
baz   one     1  2
      two     1  3
foo   one     2  4
      two     2  5
qux   one     3  6
      two     3  7

然后我们按_df_中的_second_索引级别和_A_列分组。

Then we group df by the second index level and the A column.

In [64]: df.groupby([pd.Grouper(level=1), "A"]).sum()
Out[64]:
          B
second A
one    1  2
       2  4
       3  6
two    1  4
       2  5
       3  7

索引级别也可以按名称指定。

Index levels may also be specified by name.

In [65]: df.groupby([pd.Grouper(level="second"), "A"]).sum()
Out[65]:
          B
second A
one    1  2
       2  4
       3  6
two    1  4
       2  5
       3  7

可以将索引级别名称直接指定为_groupby_的键。

Index level names may be specified as keys directly to groupby.

In [66]: df.groupby(["second", "A"]).sum()
Out[66]:
          B
second A
one    1  2
       2  4
       3  6
two    1  4
       2  5
       3  7

DataFrame column selection in GroupBy

从 DataFrame 创建 GroupBy 对象后,你可能希望对每列执行不同的操作。因此,通过以类似于从 DataFrame 获取列的方式使用 GroupBy 对象上的_[]_,你可以这样做:

Once you have created the GroupBy object from a DataFrame, you might want to do something different for each of the columns. Thus, by using [] on the GroupBy object in a similar way as the one used to get a column from a DataFrame, you can do:

In [67]: df = pd.DataFrame(
   ....:     {
   ....:         "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
   ....:         "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
   ....:         "C": np.random.randn(8),
   ....:         "D": np.random.randn(8),
   ....:     }
   ....: )
   ....:

In [68]: df
Out[68]:
     A      B         C         D
0  foo    one -0.575247  1.346061
1  bar    one  0.254161  1.511763
2  foo    two -1.143704  1.627081
3  bar  three  0.215897 -0.990582
4  foo    two  1.193555 -0.441652
5  bar    two -0.077118  1.211526
6  foo    one -0.408530  0.268520
7  foo  three -0.862495  0.024580

In [69]: grouped = df.groupby(["A"])

In [70]: grouped_C = grouped["C"]

In [71]: grouped_D = grouped["D"]

这主要是对于替代方案的语法糖,该替代方案更加冗长:

This is mainly syntactic sugar for the alternative, which is much more verbose:

In [72]: df["C"].groupby(df["A"])
Out[72]: <pandas.core.groupby.generic.SeriesGroupBy object at 0x7ff2cef1c730>

此外,此方法避免重新计算从传递密钥派生的内部分组信息。

Additionally, this method avoids recomputing the internal grouping information derived from the passed key.

你还可以包括分组列(如果你希望对其进行操作)。

You can also include the grouping columns if you want to operate on them.

In [73]: grouped[["A", "B"]].sum()
Out[73]:
                   A                  B
A
bar        barbarbar        onethreetwo
foo  foofoofoofoofoo  onetwotwoonethree

Iterating through groups

有了 GroupBy 对象,遍历分组数据非常自然,并且功能类似于 itertools.groupby()

With the GroupBy object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby():

In [74]: grouped = df.groupby('A')

In [75]: for name, group in grouped:
   ....:     print(name)
   ....:     print(group)
   ....:
bar
     A      B         C         D
1  bar    one  0.254161  1.511763
3  bar  three  0.215897 -0.990582
5  bar    two -0.077118  1.211526
foo
     A      B         C         D
0  foo    one -0.575247  1.346061
2  foo    two -1.143704  1.627081
4  foo    two  1.193555 -0.441652
6  foo    one -0.408530  0.268520
7  foo  three -0.862495  0.024580

如果按多个键分组,则组名称将是一个元组:

In the case of grouping by multiple keys, the group name will be a tuple:

In [76]: for name, group in df.groupby(['A', 'B']):
   ....:     print(name)
   ....:     print(group)
   ....:
('bar', 'one')
     A    B         C         D
1  bar  one  0.254161  1.511763
('bar', 'three')
     A      B         C         D
3  bar  three  0.215897 -0.990582
('bar', 'two')
     A    B         C         D
5  bar  two -0.077118  1.211526
('foo', 'one')
     A    B         C         D
0  foo  one -0.575247  1.346061
6  foo  one -0.408530  0.268520
('foo', 'three')
     A      B         C        D
7  foo  three -0.862495  0.02458
('foo', 'two')
     A    B         C         D
2  foo  two -1.143704  1.627081
4  foo  two  1.193555 -0.441652

Selecting a group

可以使用 DataFrameGroupBy.get_group()选择单个组:

A single group can be selected using DataFrameGroupBy.get_group():

In [77]: grouped.get_group("bar")
Out[77]:
     A      B         C         D
1  bar    one  0.254161  1.511763
3  bar  three  0.215897 -0.990582
5  bar    two -0.077118  1.211526

或者对于在多列上分组的对象:

Or for an object grouped on multiple columns:

In [78]: df.groupby(["A", "B"]).get_group(("bar", "one"))
Out[78]:
     A    B         C         D
1  bar  one  0.254161  1.511763

Aggregation

聚合是 GroupBy 操作,用于减少分组对象的维度。聚合结果是(或至少被视为)组中每列的标量值。例如,生成一组值中每列的和。

An aggregation is a GroupBy operation that reduces the dimension of the grouping object. The result of an aggregation is, or at least is treated as, a scalar value for each column in a group. For example, producing the sum of each column in a group of values.

In [79]: animals = pd.DataFrame(
   ....:     {
   ....:         "kind": ["cat", "dog", "cat", "dog"],
   ....:         "height": [9.1, 6.0, 9.5, 34.0],
   ....:         "weight": [7.9, 7.5, 9.9, 198.0],
   ....:     }
   ....: )
   ....:

In [80]: animals
Out[80]:
  kind  height  weight
0  cat     9.1     7.9
1  dog     6.0     7.5
2  cat     9.5     9.9
3  dog    34.0   198.0

In [81]: animals.groupby("kind").sum()
Out[81]:
      height  weight
kind
cat     18.6    17.8
dog     40.0   205.5

在结果中,组的键在索引中默认显示。相反,通过传递_as_index=False_,它们可以包含在列中。

In the result, the keys of the groups appear in the index by default. They can be instead included in the columns by passing as_index=False.

In [82]: animals.groupby("kind", as_index=False).sum()
Out[82]:
  kind  height  weight
0  cat    18.6    17.8
1  dog    40.0   205.5

Built-in aggregation methods

许多常见的聚合都是作为方法内置到 GroupBy 对象中的。在下面列出的方法中,带有_*_的方法没有高效的特定于 GroupBy 的实现。

Many common aggregations are built-in to GroupBy objects as methods. Of the methods listed below, those with a * do not have an efficient, GroupBy-specific, implementation.

方法

Method

说明

Description

计算组中的任何值是否为真值

Compute whether any of the values in the groups are truthy

计算组中所有值的真值

Compute whether all of the values in the groups are truthy

计算组中非 NA 值的数量

Compute the number of non-NA values in the groups

cov() *

计算组的协方差

Compute the covariance of the groups

计算每个组中第一个出现的值

Compute the first occurring value in each group

计算每个组中最大值的下标

Compute the index of the maximum value in each group

计算每个组中最小值的下标

Compute the index of the minimum value in each group

计算每个组中最后出现的值

Compute the last occurring value in each group

计算每个组中的最大值

Compute the maximum value in each group

计算每个组的平均值

Compute the mean of each group

计算每个组的中值

Compute the median of each group

计算每组中的最小值

Compute the minimum value in each group

计算每组中的唯一值的数目

Compute the number of unique values in each group

计算每组中值的乘积

Compute the product of the values in each group

计算每组中的值的给定分位数

Compute a given quantile of the values in each group

计算每组中的值的均值的标准误差

Compute the standard error of the mean of the values in each group

计算每组中的值的数目

Compute the number of values in each group

skew() *

计算每组中的值的偏度

Compute the skew of the values in each group

计算每组中的值的标准差

Compute the standard deviation of the values in each group

计算每组中值的总和

Compute the sum of the values in each group

计算每组中值的差异

Compute the variance of the values in each group

举例:

Some examples:

In [83]: df.groupby("A")[["C", "D"]].max()
Out[83]:
            C         D
A
bar  0.254161  1.511763
foo  1.193555  1.627081

In [84]: df.groupby(["A", "B"]).mean()
Out[84]:
                  C         D
A   B
bar one    0.254161  1.511763
    three  0.215897 -0.990582
    two   -0.077118  1.211526
foo one   -0.491888  0.807291
    three -0.862495  0.024580
    two    0.024925  0.592714

另一个聚合示例是计算每组的大小。它包含在 GroupBy 中,作为方法 size。它返回一个 Series,其索引由组名组成,而值是由每组的大小组成。

Another aggregation example is to compute the size of each group. This is included in GroupBy as the size method. It returns a Series whose index consists of the group names and the values are the sizes of each group.

In [85]: grouped = df.groupby(["A", "B"])

In [86]: grouped.size()
Out[86]:
A    B
bar  one      1
     three    1
     two      1
foo  one      2
     three    1
     two      2
dtype: int64

虽然方法 DataFrameGroupBy.describe() 本身不是一个还原器,但它可用于方便地生成每个组的摘要统计信息的集合。

While the DataFrameGroupBy.describe() method is not itself a reducer, it can be used to conveniently produce a collection of summary statistics about each of the groups.

In [87]: grouped.describe()
Out[87]:
              C                      ...         D
          count      mean       std  ...       50%       75%       max
A   B                                ...
bar one     1.0  0.254161       NaN  ...  1.511763  1.511763  1.511763
    three   1.0  0.215897       NaN  ... -0.990582 -0.990582 -0.990582
    two     1.0 -0.077118       NaN  ...  1.211526  1.211526  1.211526
foo one     2.0 -0.491888  0.117887  ...  0.807291  1.076676  1.346061
    three   1.0 -0.862495       NaN  ...  0.024580  0.024580  0.024580
    two     2.0  0.024925  1.652692  ...  0.592714  1.109898  1.627081

[6 rows x 16 columns]

另一个聚合示例是计算每组的唯一值的数量。它类似于函数 DataFrameGroupBy.value_counts(),除了它只计算唯一值的数目。

Another aggregation example is to compute the number of unique values of each group. This is similar to the DataFrameGroupBy.value_counts() function, except that it only counts the number of unique values.

In [88]: ll = [['foo', 1], ['foo', 2], ['foo', 2], ['bar', 1], ['bar', 1]]

In [89]: df4 = pd.DataFrame(ll, columns=["A", "B"])

In [90]: df4
Out[90]:
     A  B
0  foo  1
1  foo  2
2  foo  2
3  bar  1
4  bar  1

In [91]: df4.groupby("A")["B"].nunique()
Out[91]:
A
bar    1
foo    2
Name: B, dtype: int64

as_index=True 为默认值时,聚合函数不会将您要聚合的组作为命名列返回。分组的列将是返回对象索引。

Aggregation functions will not return the groups that you are aggregating over as named columns when as_index=True, the default. The grouped columns will be the indices of the returned object.

传递 as_index=False 将返回您要聚合的组作为命名列,无论它们是命名索引,还是输入中的列。

Passing as_index=False will return the groups that you are aggregating over as named columns, regardless if they are named indices or columns in the inputs.

The aggregate() method

方法 aggregate() 可接受类型各异的输入。本节详细介绍了用于 GroupBy 各种方法的字符串别名;其他输入在下面的各节中进行详细介绍。

The aggregate() method can accept many different types of inputs. This section details using string aliases for various GroupBy methods; other inputs are detailed in the sections below.

pandas 实现的任何还原方法都可以作为字符串传递给 aggregate()。鼓励用户使用简写形式 agg。它将操作为相应的方法被调用一般。

Any reduction method that pandas implements can be passed as a string to aggregate(). Users are encouraged to use the shorthand, agg. It will operate as if the corresponding method was called.

In [92]: grouped = df.groupby("A")

In [93]: grouped[["C", "D"]].aggregate("sum")
Out[93]:
            C         D
A
bar  0.392940  1.732707
foo -1.796421  2.824590

In [94]: grouped = df.groupby(["A", "B"])

In [95]: grouped.agg("sum")
Out[95]:
                  C         D
A   B
bar one    0.254161  1.511763
    three  0.215897 -0.990582
    two   -0.077118  1.211526
foo one   -0.983776  1.614581
    three -0.862495  0.024580
    two    0.049851  1.185429

聚合的结果会将组名作为新的索引。如果是多个键,结果默认是一个 MultiIndex。如上所述,可以通过使用选项 as_index 更改它:

The result of the aggregation will have the group names as the new index. In the case of multiple keys, the result is a MultiIndex by default. As mentioned above, this can be changed by using the as_index option:

In [96]: grouped = df.groupby(["A", "B"], as_index=False)

In [97]: grouped.agg("sum")
Out[97]:
     A      B         C         D
0  bar    one  0.254161  1.511763
1  bar  three  0.215897 -0.990582
2  bar    two -0.077118  1.211526
3  foo    one -0.983776  1.614581
4  foo  three -0.862495  0.024580
5  foo    two  0.049851  1.185429

In [98]: df.groupby("A", as_index=False)[["C", "D"]].agg("sum")
Out[98]:
     A         C         D
0  bar  0.392940  1.732707
1  foo -1.796421  2.824590

请注意,您可以使用 DataFrame.reset_index() DataFrame 函数来实现与列名相同的目标,因为列名被存储在结果 MultiIndex 中,尽管这会做一个额外的副本。

Note that you could use the DataFrame.reset_index() DataFrame function to achieve the same result as the column names are stored in the resulting MultiIndex, although this will make an extra copy.

In [99]: df.groupby(["A", "B"]).agg("sum").reset_index()
Out[99]:
     A      B         C         D
0  bar    one  0.254161  1.511763
1  bar  three  0.215897 -0.990582
2  bar    two -0.077118  1.211526
3  foo    one -0.983776  1.614581
4  foo  three -0.862495  0.024580
5  foo    two  0.049851  1.185429

Aggregation with User-Defined Functions

用户还可以提供自己的用户定义函数 (UDF),用于自定义聚合。

Users can also provide their own User-Defined Functions (UDFs) for custom aggregations.

警告

Warning

使用 UDF 进行聚合时,UDF 不应改变提供的 Series。有关更详细信息,请参阅 Mutating with User Defined Function (UDF) methods

When aggregating with a UDF, the UDF should not mutate the provided Series. See Mutating with User Defined Function (UDF) methods for more information.

使用 UDF 进行聚合通常比使用 GroupBy 的 Pandas 内置方法性能低。考虑将复杂操作分解为利用内置方法的一系列操作。

Aggregating with a UDF is often less performant than using the pandas built-in methods on GroupBy. Consider breaking up a complex operation into a chain of operations that utilize the built-in methods.

In [100]: animals
Out[100]:
  kind  height  weight
0  cat     9.1     7.9
1  dog     6.0     7.5
2  cat     9.5     9.9
3  dog    34.0   198.0

In [101]: animals.groupby("kind")[["height"]].agg(lambda x: set(x))
Out[101]:
           height
kind
cat    {9.1, 9.5}
dog   {34.0, 6.0}

结果的 dtype 将反映聚合函数的 dtype。如果不同组的结果具有不同的 dtype,那么将按照构造 DataFrame 的方式确定一个共同的 dtype。

The resulting dtype will reflect that of the aggregating function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction.

In [102]: animals.groupby("kind")[["height"]].agg(lambda x: x.astype(int).sum())
Out[102]:
      height
kind
cat       18
dog       40

Applying multiple functions at once

在已分组的 Series 上,您可以将函数列表或字典传给 SeriesGroupBy.agg(),输出一个 DataFrame:

On a grouped Series, you can pass a list or dict of functions to SeriesGroupBy.agg(), outputting a DataFrame:

In [103]: grouped = df.groupby("A")

In [104]: grouped["C"].agg(["sum", "mean", "std"])
Out[104]:
          sum      mean       std
A
bar  0.392940  0.130980  0.181231
foo -1.796421 -0.359284  0.912265

在已分组的 DataFrame 上,您可以将函数列表传给 DataFrameGroupBy.agg(),以聚合每一列,它会生成一个聚合结果和一个分级的列索引:

On a grouped DataFrame, you can pass a list of functions to DataFrameGroupBy.agg() to aggregate each column, which produces an aggregated result with a hierarchical column index:

In [105]: grouped[["C", "D"]].agg(["sum", "mean", "std"])
Out[105]:
            C                             D
          sum      mean       std       sum      mean       std
A
bar  0.392940  0.130980  0.181231  1.732707  0.577569  1.366330
foo -1.796421 -0.359284  0.912265  2.824590  0.564918  0.884785

结果的聚合以函数本身命名。如果您需要重命名,那么您可以添加一个 Series 的链式操作,如下所示:

The resulting aggregations are named after the functions themselves. If you need to rename, then you can add in a chained operation for a Series like this:

In [106]: (
   .....:     grouped["C"]
   .....:     .agg(["sum", "mean", "std"])
   .....:     .rename(columns={"sum": "foo", "mean": "bar", "std": "baz"})
   .....: )
   .....:
Out[106]:
          foo       bar       baz
A
bar  0.392940  0.130980  0.181231
foo -1.796421 -0.359284  0.912265

对于已分组的 DataFrame,您可以以类似的方式进行重命名:

For a grouped DataFrame, you can rename in a similar manner:

In [107]: (
   .....:     grouped[["C", "D"]].agg(["sum", "mean", "std"]).rename(
   .....:         columns={"sum": "foo", "mean": "bar", "std": "baz"}
   .....:     )
   .....: )
   .....:
Out[107]:
            C                             D
          foo       bar       baz       foo       bar       baz
A
bar  0.392940  0.130980  0.181231  1.732707  0.577569  1.366330
foo -1.796421 -0.359284  0.912265  2.824590  0.564918  0.884785

通常情况下,输出列名应该是唯一的,但是 pandas 允许您对同一列应用相同的函数(或具有相同名称的两个函数)。

In general, the output column names should be unique, but pandas will allow you apply to the same function (or two functions with the same name) to the same column.

In [108]: grouped["C"].agg(["sum", "sum"])
Out[108]:
          sum       sum
A
bar  0.392940  0.392940
foo -1.796421 -1.796421

pandas 还可以让你提供多个 lambda。在这种情况下,pandas 会破坏(无名字)lambda 函数的名字,将 _<i> 附加到每个后续的 lambda。

pandas also allows you to provide multiple lambdas. In this case, pandas will mangle the name of the (nameless) lambda functions, appending _<i> to each subsequent lambda.

In [109]: grouped["C"].agg([lambda x: x.max() - x.min(), lambda x: x.median() - x.mean()])
Out[109]:
     <lambda_0>  <lambda_1>
A
bar    0.331279    0.084917
foo    2.337259   -0.215962

Named aggregation

为了支持对输出列名进行控制的列特定聚合,pandas 采用了 DataFrameGroupBy.agg()SeriesGroupBy.agg() 中的特殊语法,称为“命名聚合”,其中:

To support column-specific aggregation with control over the output column names, pandas accepts the special syntax in DataFrameGroupBy.agg() and SeriesGroupBy.agg(), known as “named aggregation”, where

  1. The keywords are the output column names

  2. The values are tuples whose first element is the column to select and the second element is the aggregation to apply to that column. pandas provides the NamedAgg namedtuple with the fields ['column', 'aggfunc'] to make it clearer what the arguments are. As usual, the aggregation can be a callable or a string alias.

In [110]: animals
Out[110]:
  kind  height  weight
0  cat     9.1     7.9
1  dog     6.0     7.5
2  cat     9.5     9.9
3  dog    34.0   198.0

In [111]: animals.groupby("kind").agg(
   .....:     min_height=pd.NamedAgg(column="height", aggfunc="min"),
   .....:     max_height=pd.NamedAgg(column="height", aggfunc="max"),
   .....:     average_weight=pd.NamedAgg(column="weight", aggfunc="mean"),
   .....: )
   .....:
Out[111]:
      min_height  max_height  average_weight
kind
cat          9.1         9.5            8.90
dog          6.0        34.0          102.75

NamedAgg 只是一个 namedtuple。普通元组也是允许的。

NamedAgg is just a namedtuple. Plain tuples are allowed as well.

In [112]: animals.groupby("kind").agg(
   .....:     min_height=("height", "min"),
   .....:     max_height=("height", "max"),
   .....:     average_weight=("weight", "mean"),
   .....: )
   .....:
Out[112]:
      min_height  max_height  average_weight
kind
cat          9.1         9.5            8.90
dog          6.0        34.0          102.75

如果你想要的列名不是有效的 Python 关键字,请构造一个字典并解包关键字参数。

If the column names you want are not valid Python keywords, construct a dictionary and unpack the keyword arguments

In [113]: animals.groupby("kind").agg(
   .....:     **{
   .....:         "total weight": pd.NamedAgg(column="weight", aggfunc="sum")
   .....:     }
   .....: )
   .....:
Out[113]:
      total weight
kind
cat           17.8
dog          205.5

在使用命名聚合时,不会将附加关键字参数传递给聚合函数;只有 (column, aggfunc) 对才应该作为 **kwargs 传递。如果你的聚合函数需要附加参数,请使用 functools.partial() 部分地应用它们。

When using named aggregation, additional keyword arguments are not passed through to the aggregation functions; only pairs of (column, aggfunc) should be passed as **kwargs. If your aggregation functions require additional arguments, apply them partially with functools.partial().

命名聚合对于 Series groupby 聚合也是有效的。在这种情况下,没有列选择,因此值只是函数。

Named aggregation is also valid for Series groupby aggregations. In this case there’s no column selection, so the values are just the functions.

In [114]: animals.groupby("kind").height.agg(
   .....:     min_height="min",
   .....:     max_height="max",
   .....: )
   .....:
Out[114]:
      min_height  max_height
kind
cat          9.1         9.5
dog          6.0        34.0

Applying different functions to DataFrame columns

通过将字典传递给 aggregate,可以将不同的聚合应用到 DataFrame 的列:

By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame:

In [115]: grouped.agg({"C": "sum", "D": lambda x: np.std(x, ddof=1)})
Out[115]:
            C         D
A
bar  0.392940  1.366330
foo -1.796421  0.884785

函数名也可以是字符串。为了使字符串有效,它必须在 GroupBy 上实现:

The function names can also be strings. In order for a string to be valid it must be implemented on GroupBy:

In [116]: grouped.agg({"C": "sum", "D": "std"})
Out[116]:
            C         D
A
bar  0.392940  1.366330
foo -1.796421  0.884785

Transformation

转换是 GroupBy 操作,其结果与正在分组的结果相同。常见示例包括 cumsum()diff()

A transformation is a GroupBy operation whose result is indexed the same as the one being grouped. Common examples include cumsum() and diff().

In [117]: speeds
Out[117]:
          class           order  max_speed
falcon     bird   Falconiformes      389.0
parrot     bird  Psittaciformes       24.0
lion     mammal       Carnivora       80.2
monkey   mammal        Primates        NaN
leopard  mammal       Carnivora       58.0

In [118]: grouped = speeds.groupby("class")["max_speed"]

In [119]: grouped.cumsum()
Out[119]:
falcon     389.0
parrot     413.0
lion        80.2
monkey       NaN
leopard    138.2
Name: max_speed, dtype: float64

In [120]: grouped.diff()
Out[120]:
falcon       NaN
parrot    -365.0
lion         NaN
monkey       NaN
leopard      NaN
Name: max_speed, dtype: float64

与聚合不同,用于拆分原始对象的组不包含在结果中。

Unlike aggregations, the groupings that are used to split the original object are not included in the result.

由于转换不包括用于拆分结果的分组,因此 DataFrame.groupby()Series.groupby() 中的参数 as_indexsort 没有影响。

Since transformations do not include the groupings that are used to split the result, the arguments as_index and sort in DataFrame.groupby() and Series.groupby() have no effect.

转换的常见用途是将结果添加回原始 DataFrame。

A common use of a transformation is to add the result back into the original DataFrame.

In [121]: result = speeds.copy()

In [122]: result["cumsum"] = grouped.cumsum()

In [123]: result["diff"] = grouped.diff()

In [124]: result
Out[124]:
          class           order  max_speed  cumsum   diff
falcon     bird   Falconiformes      389.0   389.0    NaN
parrot     bird  Psittaciformes       24.0   413.0 -365.0
lion     mammal       Carnivora       80.2    80.2    NaN
monkey   mammal        Primates        NaN     NaN    NaN
leopard  mammal       Carnivora       58.0   138.2    NaN

Built-in transformation methods

GroupBy 上的以下方法充当转换。

The following methods on GroupBy act as transformations.

方法

Method

说明

Description

在各个组中回填 NA 值

Back fill NA values within each group

计算各个组内的累积计数

Compute the cumulative count within each group

计算每个组内的累积最大值

Compute the cumulative max within each group

计算每个组内的累积最小值

Compute the cumulative min within each group

计算每个组内的累积乘积

Compute the cumulative product within each group

计算每个组内的累积和

Compute the cumulative sum within each group

计算每个组内相邻值之间的差值

Compute the difference between adjacent values within each group

向前填充每个组内的 NA 值

Forward fill NA values within each group

计算每个组内相邻值之间的百分比变化

Compute the percent change between adjacent values within each group

计算每个组内每个值的排名

Compute the rank of each value within each group

在每个组内向上或向下改变值

Shift values up or down within each group

此外,将任何内置聚合方法作为字符串传递给 transform()(请参见下一部分)会在整个组内广播结果,从而产生变换后的结果。如果聚合方法具有高效的实现,那么这也会具有很高的执行效率。

In addition, passing any built-in aggregation method as a string to transform() (see the next section) will broadcast the result across the group, producing a transformed result. If the aggregation method has an efficient implementation, this will be performant as well.

The transform() method

aggregation method 类似, transform() 方法可以接受上一部分中内置变换方法的字符串别名。它还可以接受内置聚合方法的字符串别名。当提供聚合方法时,结果将在整个组内广播。

Similar to the aggregation method, the transform() method can accept string aliases to the built-in transformation methods in the previous section. It can also accept string aliases to the built-in aggregation methods. When an aggregation method is provided, the result will be broadcast across the group.

In [125]: speeds
Out[125]:
          class           order  max_speed
falcon     bird   Falconiformes      389.0
parrot     bird  Psittaciformes       24.0
lion     mammal       Carnivora       80.2
monkey   mammal        Primates        NaN
leopard  mammal       Carnivora       58.0

In [126]: grouped = speeds.groupby("class")[["max_speed"]]

In [127]: grouped.transform("cumsum")
Out[127]:
         max_speed
falcon       389.0
parrot       413.0
lion          80.2
monkey         NaN
leopard      138.2

In [128]: grouped.transform("sum")
Out[128]:
         max_speed
falcon       413.0
parrot       413.0
lion         138.2
monkey       138.2
leopard      138.2

除了字符串别名, transform() 方法还可以接受用户定义函数 (UDF)。UDF 必须:

In addition to string aliases, the transform() method can also accept User-Defined Functions (UDFs). The UDF must:

  1. Return a result that is either the same size as the group chunk or broadcastable to the size of the group chunk (e.g., a scalar, grouped.transform(lambda x: x.iloc[-1])).

  2. Operate column-by-column on the group chunk. The transform is applied to the first group chunk using chunk.apply.

  3. Not perform in-place operations on the group chunk. Group chunks should be treated as immutable, and changes to a group chunk may produce unexpected results. See Mutating with User Defined Function (UDF) methods for more information.

  4. (Optionally) operates on all columns of the entire group chunk at once. If this is supported, a fast path is used starting from the second chunk.

使用 UDF 提供 transform 的这种变换通常比使用 GroupBy 上的内置方法性能较低。考虑将复杂操作拆分成利用内置方法的一系列操作。

Transforming by supplying transform with a UDF is often less performant than using the built-in methods on GroupBy. Consider breaking up a complex operation into a chain of operations that utilize the built-in methods.

本节中的所有示例可以通过调用内置方法而不是使用 UDF 来提高性能。请参见 below for examples

All of the examples in this section can be made more performant by calling built-in methods instead of using UDFs. See below for examples.

在版本 2.0.0 中更改:使用 .transform 对分组 DataFrame 进行变换,并且变换函数返回 DataFrame 后,pandas 现在将结果的索引与输入的索引对齐。可以在变换函数中调用 .to_numpy() 以避免对齐。

Changed in version 2.0.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, pandas now aligns the result’s index with the input’s index. You can call .to_numpy() within the transformation function to avoid alignment.

The aggregate() method 类似,最终的 dtype 将反映变换函数的 dtype。如果不同组的结果具有不同的 dtype,则将以与 DataFrame 构造相同的方式确定通用 dtype。

Similar to The aggregate() method, the resulting dtype will reflect that of the transformation function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction.

假设我们希望在每个组内标准化数据:

Suppose we wish to standardize the data within each group:

In [129]: index = pd.date_range("10/1/1999", periods=1100)

In [130]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index)

In [131]: ts = ts.rolling(window=100, min_periods=100).mean().dropna()

In [132]: ts.head()
Out[132]:
2000-01-08    0.779333
2000-01-09    0.778852
2000-01-10    0.786476
2000-01-11    0.782797
2000-01-12    0.798110
Freq: D, dtype: float64

In [133]: ts.tail()
Out[133]:
2002-09-30    0.660294
2002-10-01    0.631095
2002-10-02    0.673601
2002-10-03    0.709213
2002-10-04    0.719369
Freq: D, dtype: float64

In [134]: transformed = ts.groupby(lambda x: x.year).transform(
   .....:     lambda x: (x - x.mean()) / x.std()
   .....: )
   .....:

我们希望结果在每个组内现在具有均值 0 和标准偏差 1(浮点错误除外),我们可以轻松地检查这一点:

We would expect the result to now have mean 0 and standard deviation 1 within each group (up to floating-point error), which we can easily check:

# Original Data
In [135]: grouped = ts.groupby(lambda x: x.year)

In [136]: grouped.mean()
Out[136]:
2000    0.442441
2001    0.526246
2002    0.459365
dtype: float64

In [137]: grouped.std()
Out[137]:
2000    0.131752
2001    0.210945
2002    0.128753
dtype: float64

# Transformed Data
In [138]: grouped_trans = transformed.groupby(lambda x: x.year)

In [139]: grouped_trans.mean()
Out[139]:
2000   -4.870756e-16
2001   -1.545187e-16
2002    4.136282e-16
dtype: float64

In [140]: grouped_trans.std()
Out[140]:
2000    1.0
2001    1.0
2002    1.0
dtype: float64

我们还可以直观地比较原始数据集和变换后的数据集。

We can also visually compare the original and transformed data sets.

In [141]: compare = pd.DataFrame({"Original": ts, "Transformed": transformed})

In [142]: compare.plot()
Out[142]: <Axes: >

输出维度较低的变换函数被广播以匹配输入数组的形状。

Transformation functions that have lower dimension outputs are broadcast to match the shape of the input array.

In [143]: ts.groupby(lambda x: x.year).transform(lambda x: x.max() - x.min())
Out[143]:
2000-01-08    0.623893
2000-01-09    0.623893
2000-01-10    0.623893
2000-01-11    0.623893
2000-01-12    0.623893
                ...
2002-09-30    0.558275
2002-10-01    0.558275
2002-10-02    0.558275
2002-10-03    0.558275
2002-10-04    0.558275
Freq: D, Length: 1001, dtype: float64

另一个常见的数据变换是用组均值替换缺失数据。

Another common data transform is to replace missing data with the group mean.

In [144]: cols = ["A", "B", "C"]

In [145]: values = np.random.randn(1000, 3)

In [146]: values[np.random.randint(0, 1000, 100), 0] = np.nan

In [147]: values[np.random.randint(0, 1000, 50), 1] = np.nan

In [148]: values[np.random.randint(0, 1000, 200), 2] = np.nan

In [149]: data_df = pd.DataFrame(values, columns=cols)

In [150]: data_df
Out[150]:
            A         B         C
0    1.539708 -1.166480  0.533026
1    1.302092 -0.505754       NaN
2   -0.371983  1.104803 -0.651520
3   -1.309622  1.118697 -1.161657
4   -1.924296  0.396437  0.812436
..        ...       ...       ...
995 -0.093110  0.683847 -0.774753
996 -0.185043  1.438572       NaN
997 -0.394469 -0.642343  0.011374
998 -1.174126  1.857148       NaN
999  0.234564  0.517098  0.393534

[1000 rows x 3 columns]

In [151]: countries = np.array(["US", "UK", "GR", "JP"])

In [152]: key = countries[np.random.randint(0, 4, 1000)]

In [153]: grouped = data_df.groupby(key)

# Non-NA count in each group
In [154]: grouped.count()
Out[154]:
      A    B    C
GR  209  217  189
JP  240  255  217
UK  216  231  193
US  239  250  217

In [155]: transformed = grouped.transform(lambda x: x.fillna(x.mean()))

我们可以验证组均值在变换后的数据中没有变化,并且变换后的数据不包含 NA。

We can verify that the group means have not changed in the transformed data, and that the transformed data contains no NAs.

In [156]: grouped_trans = transformed.groupby(key)

In [157]: grouped.mean()  # original group means
Out[157]:
           A         B         C
GR -0.098371 -0.015420  0.068053
JP  0.069025  0.023100 -0.077324
UK  0.034069 -0.052580 -0.116525
US  0.058664 -0.020399  0.028603

In [158]: grouped_trans.mean()  # transformation did not change group means
Out[158]:
           A         B         C
GR -0.098371 -0.015420  0.068053
JP  0.069025  0.023100 -0.077324
UK  0.034069 -0.052580 -0.116525
US  0.058664 -0.020399  0.028603

In [159]: grouped.count()  # original has some missing data points
Out[159]:
      A    B    C
GR  209  217  189
JP  240  255  217
UK  216  231  193
US  239  250  217

In [160]: grouped_trans.count()  # counts after transformation
Out[160]:
      A    B    C
GR  228  228  228
JP  267  267  267
UK  247  247  247
US  258  258  258

In [161]: grouped_trans.size()  # Verify non-NA count equals group size
Out[161]:
GR    228
JP    267
UK    247
US    258
dtype: int64

如上文所述,本节中的每个示例都可以使用内置方法更有效地计算。在以下代码中,使用 UDF 的低效方法已注释掉,更快的替代方法显示在下。

As mentioned in the note above, each of the examples in this section can be computed more efficiently using built-in methods. In the code below, the inefficient way using a UDF is commented out and the faster alternative appears below.

# result = ts.groupby(lambda x: x.year).transform(
#     lambda x: (x - x.mean()) / x.std()
# )
In [162]: grouped = ts.groupby(lambda x: x.year)

In [163]: result = (ts - grouped.transform("mean")) / grouped.transform("std")

# result = ts.groupby(lambda x: x.year).transform(lambda x: x.max() - x.min())
In [164]: grouped = ts.groupby(lambda x: x.year)

In [165]: result = grouped.transform("max") - grouped.transform("min")

# grouped = data_df.groupby(key)
# result = grouped.transform(lambda x: x.fillna(x.mean()))
In [166]: grouped = data_df.groupby(key)

In [167]: result = data_df.fillna(grouped.transform("mean"))

Window and resample operations

可以使用 resample()expanding()rolling() 作为组的对象方法。

It is possible to use resample(), expanding() and rolling() as methods on groupbys.

下面的示例将基于列 A 的组,对列 B 的样本应用 rolling() 方法。

The example below will apply the rolling() method on the samples of the column B, based on the groups of column A.

In [168]: df_re = pd.DataFrame({"A": [1] * 10 + [5] * 10, "B": np.arange(20)})

In [169]: df_re
Out[169]:
    A   B
0   1   0
1   1   1
2   1   2
3   1   3
4   1   4
.. ..  ..
15  5  15
16  5  16
17  5  17
18  5  18
19  5  19

[20 rows x 2 columns]

In [170]: df_re.groupby("A").rolling(4).B.mean()
Out[170]:
A
1  0      NaN
   1      NaN
   2      NaN
   3      1.5
   4      2.5
         ...
5  15    13.5
   16    14.5
   17    15.5
   18    16.5
   19    17.5
Name: B, Length: 20, dtype: float64

expanding() 方法将为每个特别组的所有成员积累给定操作(本例中为 sum())。

The expanding() method will accumulate a given operation (sum() in the example) for all the members of each particular group.

In [171]: df_re.groupby("A").expanding().sum()
Out[171]:
          B
A
1 0     0.0
  1     1.0
  2     3.0
  3     6.0
  4    10.0
...     ...
5 15   75.0
  16   91.0
  17  108.0
  18  126.0
  19  145.0

[20 rows x 1 columns]

假设您想使用 resample() 方法在您数据框架的每个组中获取一个日频率,并且希望用 ffill() 方法补全缺失值。

Suppose you want to use the resample() method to get a daily frequency in each group of your dataframe, and wish to complete the missing values with the ffill() method.

In [172]: df_re = pd.DataFrame(
   .....:     {
   .....:         "date": pd.date_range(start="2016-01-01", periods=4, freq="W"),
   .....:         "group": [1, 1, 2, 2],
   .....:         "val": [5, 6, 7, 8],
   .....:     }
   .....: ).set_index("date")
   .....:

In [173]: df_re
Out[173]:
            group  val
date
2016-01-03      1    5
2016-01-10      1    6
2016-01-17      2    7
2016-01-24      2    8

In [174]: df_re.groupby("group").resample("1D", include_groups=False).ffill()
Out[174]:
                  val
group date
1     2016-01-03    5
      2016-01-04    5
      2016-01-05    5
      2016-01-06    5
      2016-01-07    5
...               ...
2     2016-01-20    7
      2016-01-21    7
      2016-01-22    7
      2016-01-23    7
      2016-01-24    8

[16 rows x 1 columns]

Filtration

过滤是一项 GroupBy 操作,它对原始分组对象进行子集化。它可以过滤出整个组、组的部分或两者。过滤将返回调用对象的一个经过过滤的版本,包括在已提供的情况下对分组列。在以下示例中,class 包含在结果中。

A filtration is a GroupBy operation that subsets the original grouping object. It may either filter out entire groups, part of groups, or both. Filtrations return a filtered version of the calling object, including the grouping columns when provided. In the following example, class is included in the result.

In [175]: speeds
Out[175]:
          class           order  max_speed
falcon     bird   Falconiformes      389.0
parrot     bird  Psittaciformes       24.0
lion     mammal       Carnivora       80.2
monkey   mammal        Primates        NaN
leopard  mammal       Carnivora       58.0

In [176]: speeds.groupby("class").nth(1)
Out[176]:
         class           order  max_speed
parrot    bird  Psittaciformes       24.0
monkey  mammal        Primates        NaN

与聚合不同,过滤不会将组键添加到结果的索引。因此,传递 as_index=Falsesort=True 不会影响这些方法。

Unlike aggregations, filtrations do not add the group keys to the index of the result. Because of this, passing as_index=False or sort=True will not affect these methods.

过滤会尊重 GroupBy 对象的列子集化。

Filtrations will respect subsetting the columns of the GroupBy object.

In [177]: speeds.groupby("class")[["order", "max_speed"]].nth(1)
Out[177]:
                 order  max_speed
parrot  Psittaciformes       24.0
monkey        Primates        NaN

Built-in filtrations

以下 GroupBy 方法作为过滤作用。所有这些方法都有一个高效的、特定于 GroupBy 的实现。

The following methods on GroupBy act as filtrations. All these methods have an efficient, GroupBy-specific, implementation.

方法

Method

说明

Description

选择每个组的顶部行

Select the top row(s) of each group

选择每个组的第 n 行

Select the nth row(s) of each group

选择每个组的底部行

Select the bottom row(s) of each group

用户还可以将转换与布尔索引一起使用,以便在组内构建复杂的过滤。例如,假设我们给出了产品组及其体积,并且我们希望将数据子集化到不超过每个组内总体积 90% 的仅限最大产品上。

Users can also use transformations along with Boolean indexing to construct complex filtrations within groups. For example, suppose we are given groups of products and their volumes, and we wish to subset the data to only the largest products capturing no more than 90% of the total volume within each group.

In [178]: product_volumes = pd.DataFrame(
   .....:     {
   .....:         "group": list("xxxxyyy"),
   .....:         "product": list("abcdefg"),
   .....:         "volume": [10, 30, 20, 15, 40, 10, 20],
   .....:     }
   .....: )
   .....:

In [179]: product_volumes
Out[179]:
  group product  volume
0     x       a      10
1     x       b      30
2     x       c      20
3     x       d      15
4     y       e      40
5     y       f      10
6     y       g      20

# Sort by volume to select the largest products first
In [180]: product_volumes = product_volumes.sort_values("volume", ascending=False)

In [181]: grouped = product_volumes.groupby("group")["volume"]

In [182]: cumpct = grouped.cumsum() / grouped.transform("sum")

In [183]: cumpct
Out[183]:
4    0.571429
1    0.400000
2    0.666667
6    0.857143
3    0.866667
0    1.000000
5    1.000000
Name: volume, dtype: float64

In [184]: significant_products = product_volumes[cumpct <= 0.9]

In [185]: significant_products.sort_values(["group", "product"])
Out[185]:
  group product  volume
1     x       b      30
2     x       c      20
3     x       d      15
4     y       e      40
6     y       g      20

The filter method

通过使用用户自定义函数 (UDF) 向 filter 提供过滤通常比在 GroupBy 上使用内置方法的性能要低。考虑将复杂操作分解为一系列利用内置方法的操作。

Filtering by supplying filter with a User-Defined Function (UDF) is often less performant than using the built-in methods on GroupBy. Consider breaking up a complex operation into a chain of operations that utilize the built-in methods.

filter 方法采用一个用户自定义函数 (UDF),当对整个组应用该函数时,返回 TrueFalse。然后,filter 方法的结果是 UDF 返回 True 的组的子集。

The filter method takes a User-Defined Function (UDF) that, when applied to an entire group, returns either True or False. The result of the filter method is then the subset of groups for which the UDF returned True.

假设我们只想获取属于组和大于 2 的组和的元素。

Suppose we want to take only elements that belong to groups with a group sum greater than 2.

In [186]: sf = pd.Series([1, 1, 2, 3, 3, 3])

In [187]: sf.groupby(sf).filter(lambda x: x.sum() > 2)
Out[187]:
3    3
4    3
5    3
dtype: int64

另一个有用的操作是过滤组中仅有几个成员的元素。

Another useful operation is filtering out elements that belong to groups with only a couple members.

In [188]: dff = pd.DataFrame({"A": np.arange(8), "B": list("aabbbbcc")})

In [189]: dff.groupby("B").filter(lambda x: len(x) > 2)
Out[189]:
   A  B
2  2  b
3  3  b
4  4  b
5  5  b

另外,我们可以返回一个以相同索引为索引的对象,其中不通过过滤器的组用 NaN 填充,而不是删除有问题的组。

Alternatively, instead of dropping the offending groups, we can return a like-indexed objects where the groups that do not pass the filter are filled with NaNs.

In [190]: dff.groupby("B").filter(lambda x: len(x) > 2, dropna=False)
Out[190]:
     A    B
0  NaN  NaN
1  NaN  NaN
2  2.0    b
3  3.0    b
4  4.0    b
5  5.0    b
6  NaN  NaN
7  NaN  NaN

对于具有多列的数据框,过滤器应明确指定一个列作为过滤器标准。

For DataFrames with multiple columns, filters should explicitly specify a column as the filter criterion.

In [191]: dff["C"] = np.arange(8)

In [192]: dff.groupby("B").filter(lambda x: len(x["C"]) > 2)
Out[192]:
   A  B  C
2  2  b  2
3  3  b  3
4  4  b  4
5  5  b  5

Flexible apply

对分组数据的某些操作可能不适合于聚合、转换或过滤类别。对于这些操作,您可以使用 apply 函数。

Some operations on the grouped data might not fit into the aggregation, transformation, or filtration categories. For these, you can use the apply function.

警告

Warning

apply 必须尝试从结果中推断出应当作为reducer、transformer还是filter,具体取决于传递给它的内容。因此,分组列可能包含在输出中,也可能不包含。虽然它尝试智能猜测如何表现,但有时可能猜测错误。

apply has to try to infer from the result whether it should act as a reducer, transformer, or filter, depending on exactly what is passed to it. Thus the grouped column(s) may be included in the output or not. While it tries to intelligently guess how to behave, it can sometimes guess wrong.

本节中的所有示例都可以使用其他pandas功能以更可靠、更高效的方式进行计算。

All of the examples in this section can be more reliably, and more efficiently, computed using other pandas functionality.

In [193]: df
Out[193]:
     A      B         C         D
0  foo    one -0.575247  1.346061
1  bar    one  0.254161  1.511763
2  foo    two -1.143704  1.627081
3  bar  three  0.215897 -0.990582
4  foo    two  1.193555 -0.441652
5  bar    two -0.077118  1.211526
6  foo    one -0.408530  0.268520
7  foo  three -0.862495  0.024580

In [194]: grouped = df.groupby("A")

# could also just call .describe()
In [195]: grouped["C"].apply(lambda x: x.describe())
Out[195]:
A
bar  count    3.000000
     mean     0.130980
     std      0.181231
     min     -0.077118
     25%      0.069390
                ...
foo  min     -1.143704
     25%     -0.862495
     50%     -0.575247
     75%     -0.408530
     max      1.193555
Name: C, Length: 16, dtype: float64

返回结果的维度也可能发生变化:

The dimension of the returned result can also change:

In [196]: grouped = df.groupby('A')['C']

In [197]: def f(group):
   .....:     return pd.DataFrame({'original': group,
   .....:                          'demeaned': group - group.mean()})
   .....:

In [198]: grouped.apply(f)
Out[198]:
       original  demeaned
A
bar 1  0.254161  0.123181
    3  0.215897  0.084917
    5 -0.077118 -0.208098
foo 0 -0.575247 -0.215962
    2 -1.143704 -0.784420
    4  1.193555  1.552839
    6 -0.408530 -0.049245
    7 -0.862495 -0.503211

apply 在series上可以对apply函数返回的本身为series的返回值进行操作,并且可能将结果提升为DataFrame:

apply on a Series can operate on a returned value from the applied function that is itself a series, and possibly upcast the result to a DataFrame:

In [199]: def f(x):
   .....:     return pd.Series([x, x ** 2], index=["x", "x^2"])
   .....:

In [200]: s = pd.Series(np.random.rand(5))

In [201]: s
Out[201]:
0    0.582898
1    0.098352
2    0.001438
3    0.009420
4    0.815826
dtype: float64

In [202]: s.apply(f)
Out[202]:
          x       x^2
0  0.582898  0.339770
1  0.098352  0.009673
2  0.001438  0.000002
3  0.009420  0.000089
4  0.815826  0.665572

类似于 The aggregate() method,结果的dtype将反映apply函数的dtype。如果来自不同组的结果具有不同的dtype,则将以与_DataFrame_构造相同的方式确定公共dtype。

Similar to The aggregate() method, the resulting dtype will reflect that of the apply function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction.

Control grouped column(s) placement with group_keys

要控制是否将分组列包含在索引中,可以使用参数_group_keys_,其默认为_True_。比较

To control whether the grouped column(s) are included in the indices, you can use the argument group_keys which defaults to True. Compare

In [203]: df.groupby("A", group_keys=True).apply(lambda x: x, include_groups=False)
Out[203]:
           B         C         D
A
bar 1    one  0.254161  1.511763
    3  three  0.215897 -0.990582
    5    two -0.077118  1.211526
foo 0    one -0.575247  1.346061
    2    two -1.143704  1.627081
    4    two  1.193555 -0.441652
    6    one -0.408530  0.268520
    7  three -0.862495  0.024580

带有

with

In [204]: df.groupby("A", group_keys=False).apply(lambda x: x, include_groups=False)
Out[204]:
       B         C         D
0    one -0.575247  1.346061
1    one  0.254161  1.511763
2    two -1.143704  1.627081
3  three  0.215897 -0.990582
4    two  1.193555 -0.441652
5    two -0.077118  1.211526
6    one -0.408530  0.268520
7  three -0.862495  0.024580

Numba Accelerated Routines

1.1版中的新增功能。

New in version 1.1.

如果 Numba作为可选依赖项安装,则方法_transform_和_aggregate_支持参数_engine='numba'和_engine_kwargs。请参阅 enhancing performance with Numba以了解参数的一般用法和性能注意事项。

If Numba is installed as an optional dependency, the transform and aggregate methods support engine='numba' and engine_kwargs arguments. See enhancing performance with Numba for general usage of the arguments and performance considerations.

函数签名必须以_values,_ index_开头,具体取决于如何将属于每个组的数据传递到_values_以及如何将组索引传递到_index

The function signature must start with values, index exactly as the data belonging to each group will be passed into values, and the group index will be passed into index.

警告

Warning

在使用_engine='numba'_时,内部将没有“后备”行为。组数据和组索引将作为NumPy数组传递到JIT化用户定义函数,并且不会尝试其他执行尝试。

When using engine='numba', there will be no “fall back” behavior internally. The group data and group index will be passed as NumPy arrays to the JITed user defined function, and no alternative execution attempts will be tried.

Other useful features

Exclusion of non-numeric columns

再次考虑我们已经观察过的示例DataFrame:

Again consider the example DataFrame we’ve been looking at:

In [205]: df
Out[205]:
     A      B         C         D
0  foo    one -0.575247  1.346061
1  bar    one  0.254161  1.511763
2  foo    two -1.143704  1.627081
3  bar  three  0.215897 -0.990582
4  foo    two  1.193555 -0.441652
5  bar    two -0.077118  1.211526
6  foo    one -0.408530  0.268520
7  foo  three -0.862495  0.024580

假设我们希望按_A_列计算标准差。这里有个小问题,即我们不在乎列_B_中的数据,因为它不是数字。你可以通过指定_numeric_only=True_来避免非数字列:

Suppose we wish to compute the standard deviation grouped by the A column. There is a slight problem, namely that we don’t care about the data in column B because it is not numeric. You can avoid non-numeric columns by specifying numeric_only=True:

In [206]: df.groupby("A").std(numeric_only=True)
Out[206]:
            C         D
A
bar  0.181231  1.366330
foo  0.912265  0.884785

注意,df.groupby('A').colname.std()._比_df.groupby('A').std().colname_更有效率。因此,如果聚合函数的结果只需要在一列(此处为_colname)上,则可以在应用聚合函数之前对其进行筛选。

Note that df.groupby('A').colname.std(). is more efficient than df.groupby('A').std().colname. So if the result of an aggregation function is only needed over one column (here colname), it may be filtered before applying the aggregation function.

In [207]: from decimal import Decimal

In [208]: df_dec = pd.DataFrame(
   .....:     {
   .....:         "id": [1, 2, 1, 2],
   .....:         "int_column": [1, 2, 3, 4],
   .....:         "dec_column": [
   .....:             Decimal("0.50"),
   .....:             Decimal("0.15"),
   .....:             Decimal("0.25"),
   .....:             Decimal("0.40"),
   .....:         ],
   .....:     }
   .....: )
   .....:

In [209]: df_dec.groupby(["id"])[["dec_column"]].sum()
Out[209]:
   dec_column
id
1        0.75
2        0.55

Handling of (un)observed Categorical values

在使用_Categorical_分组器(作为单个分组器或作为多个分组器的一部分)时,observed_关键字控制是否返回所有可能的分组器值的笛卡尔积(_observed=False)或仅返回那些观测分组器(observed=True)。

When using a Categorical grouper (as a single grouper, or as part of multiple groupers), the observed keyword controls whether to return a cartesian product of all possible groupers values (observed=False) or only those that are observed groupers (observed=True).

显示所有值:

Show all values:

In [210]: pd.Series([1, 1, 1]).groupby(
   .....:     pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False
   .....: ).count()
   .....:
Out[210]:
a    3
b    0
dtype: int64

仅显示观察到的值:

Show only the observed values:

In [211]: pd.Series([1, 1, 1]).groupby(
   .....:     pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=True
   .....: ).count()
   .....:
Out[211]:
a    3
dtype: int64

分组的返回dtype将始终包括所有已分组的类别。

The returned dtype of the grouped will always include all of the categories that were grouped.

In [212]: s = (
   .....:     pd.Series([1, 1, 1])
   .....:     .groupby(pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=True)
   .....:     .count()
   .....: )
   .....:

In [213]: s.index.dtype
Out[213]: CategoricalDtype(categories=['a', 'b'], ordered=False, categories_dtype=object)

NA group handling

通过_NA_,我们指的是任何_NA_值,包括 NANaNNaT_和_None。如果分组键中存在任何_NA_值,则默认情况下将排除这些值。换句话说,将删除任何“_NA_组”。你可以通过指定_dropna=False_来包含NA组。

By NA, we are referring to any NA values, including NA, NaN, NaT, and None. If there are any NA values in the grouping key, by default these will be excluded. In other words, any “NA group” will be dropped. You can include NA groups by specifying dropna=False.

In [214]: df = pd.DataFrame({"key": [1.0, 1.0, np.nan, 2.0, np.nan], "A": [1, 2, 3, 4, 5]})

In [215]: df
Out[215]:
   key  A
0  1.0  1
1  1.0  2
2  NaN  3
3  2.0  4
4  NaN  5

In [216]: df.groupby("key", dropna=True).sum()
Out[216]:
     A
key
1.0  3
2.0  4

In [217]: df.groupby("key", dropna=False).sum()
Out[217]:
     A
key
1.0  3
2.0  4
NaN  8

Grouping with ordered factors

可以将表示为pandas的_Categorical_类的实例的分类变量用作组键。如果是这样,则级别的顺序将保留。当_observed=False_和_sort=False_时,任何未观测的类别将按顺序位于结果的末尾。

Categorical variables represented as instances of pandas’s Categorical class can be used as group keys. If so, the order of the levels will be preserved. When observed=False and sort=False, any unobserved categories will be at the end of the result in order.

In [218]: days = pd.Categorical(
   .....:     values=["Wed", "Mon", "Thu", "Mon", "Wed", "Sat"],
   .....:     categories=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
   .....: )
   .....:

In [219]: data = pd.DataFrame(
   .....:    {
   .....:        "day": days,
   .....:        "workers": [3, 4, 1, 4, 2, 2],
   .....:    }
   .....: )
   .....:

In [220]: data
Out[220]:
   day  workers
0  Wed        3
1  Mon        4
2  Thu        1
3  Mon        4
4  Wed        2
5  Sat        2

In [221]: data.groupby("day", observed=False, sort=True).sum()
Out[221]:
     workers
day
Mon        8
Tue        0
Wed        5
Thu        1
Fri        0
Sat        2
Sun        0

In [222]: data.groupby("day", observed=False, sort=False).sum()
Out[222]:
     workers
day
Wed        5
Mon        8
Thu        1
Sat        2
Tue        0
Fri        0
Sun        0

Grouping with a grouper specification

你可以指定更多数据来正确分组。你可以使用 pd.Grouper 提供本地控制。

You may need to specify a bit more data to properly group. You can use the pd.Grouper to provide this local control.

In [223]: import datetime

In [224]: df = pd.DataFrame(
   .....:     {
   .....:         "Branch": "A A A A A A A B".split(),
   .....:         "Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(),
   .....:         "Quantity": [1, 3, 5, 1, 8, 1, 9, 3],
   .....:         "Date": [
   .....:             datetime.datetime(2013, 1, 1, 13, 0),
   .....:             datetime.datetime(2013, 1, 1, 13, 5),
   .....:             datetime.datetime(2013, 10, 1, 20, 0),
   .....:             datetime.datetime(2013, 10, 2, 10, 0),
   .....:             datetime.datetime(2013, 10, 1, 20, 0),
   .....:             datetime.datetime(2013, 10, 2, 10, 0),
   .....:             datetime.datetime(2013, 12, 2, 12, 0),
   .....:             datetime.datetime(2013, 12, 2, 14, 0),
   .....:         ],
   .....:     }
   .....: )
   .....:

In [225]: df
Out[225]:
  Branch Buyer  Quantity                Date
0      A  Carl         1 2013-01-01 13:00:00
1      A  Mark         3 2013-01-01 13:05:00
2      A  Carl         5 2013-10-01 20:00:00
3      A  Carl         1 2013-10-02 10:00:00
4      A   Joe         8 2013-10-01 20:00:00
5      A   Joe         1 2013-10-02 10:00:00
6      A   Joe         9 2013-12-02 12:00:00
7      B  Carl         3 2013-12-02 14:00:00

按特定列使用所需频率进行分组。这就像重新采样。

Groupby a specific column with the desired frequency. This is like resampling.

In [226]: df.groupby([pd.Grouper(freq="1ME", key="Date"), "Buyer"])[["Quantity"]].sum()
Out[226]:
                  Quantity
Date       Buyer
2013-01-31 Carl          1
           Mark          3
2013-10-31 Carl          6
           Joe           9
2013-12-31 Carl          3
           Joe           9

freq 被指定时,pd.Grouper 返回的对象将 pandas.api.typing.TimeGrouper 的一个实例。当有列和同名索引时,你可以使用 key 按列进行分组和 level 按索引进行分组。

When freq is specified, the object returned by pd.Grouper will be an instance of pandas.api.typing.TimeGrouper. When there is a column and index with the same name, you can use key to group by the column and level to group by the index.

In [227]: df = df.set_index("Date")

In [228]: df["Date"] = df.index + pd.offsets.MonthEnd(2)

In [229]: df.groupby([pd.Grouper(freq="6ME", key="Date"), "Buyer"])[["Quantity"]].sum()
Out[229]:
                  Quantity
Date       Buyer
2013-02-28 Carl          1
           Mark          3
2014-02-28 Carl          9
           Joe          18

In [230]: df.groupby([pd.Grouper(freq="6ME", level="Date"), "Buyer"])[["Quantity"]].sum()
Out[230]:
                  Quantity
Date       Buyer
2013-01-31 Carl          1
           Mark          3
2014-01-31 Carl          9
           Joe          18

Taking the first rows of each group

就 DataFrame 或 Series 而言,你可以按组调用头部和尾部:

Just like for a DataFrame or Series you can call head and tail on a groupby:

In [231]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"])

In [232]: df
Out[232]:
   A  B
0  1  2
1  1  4
2  5  6

In [233]: g = df.groupby("A")

In [234]: g.head(1)
Out[234]:
   A  B
0  1  2
2  5  6

In [235]: g.tail(1)
Out[235]:
   A  B
1  1  4
2  5  6

这显示每组的第一个或最后 n 行。

This shows the first or last n rows from each group.

Taking the nth row of each group

要从每组中选择第 n 项,请使用 DataFrameGroupBy.nth()SeriesGroupBy.nth()。提供的参数可以是任何整数、整数列表、切片或切片列表;有关示例,请参阅以下内容。当组中的第 n 个元素不存在时,不会引发错误;相反,不会返回相应的行。

To select the nth item from each group, use DataFrameGroupBy.nth() or SeriesGroupBy.nth(). Arguments supplied can be any integer, lists of integers, slices, or lists of slices; see below for examples. When the nth element of a group does not exist an error is not raised; instead no corresponding rows are returned.

通常,此操作充当一个筛选条件。在某些情况下,它还将返回每组一行,从而使其也成为一个简化操作。但是,由于通常一个组中可以返回零或多行,因此 pandas 在所有情况下都将其视为筛选条件。

In general this operation acts as a filtration. In certain cases it will also return one row per group, making it also a reduction. However because in general it can return zero or multiple rows per group, pandas treats it as a filtration in all cases.

In [236]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"])

In [237]: g = df.groupby("A")

In [238]: g.nth(0)
Out[238]:
   A    B
0  1  NaN
2  5  6.0

In [239]: g.nth(-1)
Out[239]:
   A    B
1  1  4.0
2  5  6.0

In [240]: g.nth(1)
Out[240]:
   A    B
1  1  4.0

如果组的第 n 个元素不存在,则结果中不会包含相应的行。特别是,如果指定的 n 大于任何组,则结果将是一个空 DataFrame。

If the nth element of a group does not exist, then no corresponding row is included in the result. In particular, if the specified n is larger than any group, the result will be an empty DataFrame.

In [241]: g.nth(5)
Out[241]:
Empty DataFrame
Columns: [A, B]
Index: []

如果你想选择第 n 个非空项,请使用 dropna kwarg。对于一个 DataFrame,这应是 'any''all',就像你传递给 dropna 的一样:

If you want to select the nth not-null item, use the dropna kwarg. For a DataFrame this should be either 'any' or 'all' just like you would pass to dropna:

# nth(0) is the same as g.first()
In [242]: g.nth(0, dropna="any")
Out[242]:
   A    B
1  1  4.0
2  5  6.0

In [243]: g.first()
Out[243]:
     B
A
1  4.0
5  6.0

# nth(-1) is the same as g.last()
In [244]: g.nth(-1, dropna="any")
Out[244]:
   A    B
1  1  4.0
2  5  6.0

In [245]: g.last()
Out[245]:
     B
A
1  4.0
5  6.0

In [246]: g.B.nth(0, dropna="all")
Out[246]:
1    4.0
2    6.0
Name: B, dtype: float64

你还可以通过指定多个第 n 个值(作为整数列表)来从每组中选择多行。

You can also select multiple rows from each group by specifying multiple nth values as a list of ints.

In [247]: business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B")

In [248]: df = pd.DataFrame(1, index=business_dates, columns=["a", "b"])

# get the first, 4th, and last date index for each month
In [249]: df.groupby([df.index.year, df.index.month]).nth([0, 3, -1])
Out[249]:
            a  b
2014-04-01  1  1
2014-04-04  1  1
2014-04-30  1  1
2014-05-01  1  1
2014-05-06  1  1
2014-05-30  1  1
2014-06-02  1  1
2014-06-05  1  1
2014-06-30  1  1

你还可以使用切片或切片列表。

You may also use slices or lists of slices.

In [250]: df.groupby([df.index.year, df.index.month]).nth[1:]
Out[250]:
            a  b
2014-04-02  1  1
2014-04-03  1  1
2014-04-04  1  1
2014-04-07  1  1
2014-04-08  1  1
...        .. ..
2014-06-24  1  1
2014-06-25  1  1
2014-06-26  1  1
2014-06-27  1  1
2014-06-30  1  1

[62 rows x 2 columns]

In [251]: df.groupby([df.index.year, df.index.month]).nth[1:, :-1]
Out[251]:
            a  b
2014-04-01  1  1
2014-04-02  1  1
2014-04-03  1  1
2014-04-04  1  1
2014-04-07  1  1
...        .. ..
2014-06-24  1  1
2014-06-25  1  1
2014-06-26  1  1
2014-06-27  1  1
2014-06-30  1  1

[65 rows x 2 columns]

Enumerate group items

要查看每行在组中出现的顺序,请使用 cumcount 方法:

To see the order in which each row appears within its group, use the cumcount method:

In [252]: dfg = pd.DataFrame(list("aaabba"), columns=["A"])

In [253]: dfg
Out[253]:
   A
0  a
1  a
2  a
3  b
4  b
5  a

In [254]: dfg.groupby("A").cumcount()
Out[254]:
0    0
1    1
2    2
3    0
4    1
5    3
dtype: int64

In [255]: dfg.groupby("A").cumcount(ascending=False)
Out[255]:
0    3
1    2
2    1
3    1
4    0
5    0
dtype: int64

Enumerate groups

要查看组的顺序(与由 cumcount 指定的组内行顺序相反),你可以使用 DataFrameGroupBy.ngroup()

To see the ordering of the groups (as opposed to the order of rows within a group given by cumcount) you can use DataFrameGroupBy.ngroup().

注意,给组的号码与遍历 groupby 对象时看到组的顺序相符,而不是第一次观察到的顺序。

Note that the numbers given to the groups match the order in which the groups would be seen when iterating over the groupby object, not the order they are first observed.

In [256]: dfg = pd.DataFrame(list("aaabba"), columns=["A"])

In [257]: dfg
Out[257]:
   A
0  a
1  a
2  a
3  b
4  b
5  a

In [258]: dfg.groupby("A").ngroup()
Out[258]:
0    0
1    0
2    0
3    1
4    1
5    0
dtype: int64

In [259]: dfg.groupby("A").ngroup(ascending=False)
Out[259]:
0    1
1    1
2    1
3    0
4    0
5    1
dtype: int64

Plotting

Groupby 还可以配合一些绘图方法使用。在这种情况下,假设我们怀疑第 1 列中的值在组“B”中平均高出 3 倍。

Groupby also works with some plotting methods. In this case, suppose we suspect that the values in column 1 are 3 times higher on average in group “B”.

In [260]: np.random.seed(1234)

In [261]: df = pd.DataFrame(np.random.randn(50, 2))

In [262]: df["g"] = np.random.choice(["A", "B"], size=50)

In [263]: df.loc[df["g"] == "B", 1] += 3

我们可以使用箱线图轻松视化此内容:

We can easily visualize this with a boxplot:

In [264]: df.groupby("g").boxplot()
Out[264]:
A         Axes(0.1,0.15;0.363636x0.75)
B    Axes(0.536364,0.15;0.363636x0.75)
dtype: object

调用 boxplot 的结果是一个字典,其键是我们的分组列 g 的值(“A”和“B”)。可通过 boxplotreturn_type 关键字控制结果字典的值。请参阅 visualization documentation 了解更多信息。

The result of calling boxplot is a dictionary whose keys are the values of our grouping column g (“A” and “B”). The values of the resulting dictionary can be controlled by the return_type keyword of boxplot. See the visualization documentation for more.

警告

Warning

出于历史原因,df.groupby("g").boxplot() 不等于 df.boxplot(by="g")。有关解释,请参阅 here

For historical reasons, df.groupby("g").boxplot() is not equivalent to df.boxplot(by="g"). See here for an explanation.

Piping function calls

与由 DataFrameSeries 提供的功能类似,可以将获取 GroupBy 对象的函数使用 pipe 方法链接在一起,以允许更简洁更可读的语法。要了解一般意义上的 .pipe,请参阅 here

Similar to the functionality provided by DataFrame and Series, functions that take GroupBy objects can be chained together using a pipe method to allow for a cleaner, more readable syntax. To read about .pipe in general terms, see here.

.groupby.pipe 相结合通常在你需要重复使用 GroupBy 对象时很有用。

Combining .groupby and .pipe is often useful when you need to reuse GroupBy objects.

作为一个例子,想象一下有一个 DataFrame,其中的列用于存储、产品、收入和所售数量。我们希望执行一个按每一商店和每一产品分组的计算,计算每单位价格(即 收入/数量)。我们可以在一个多步骤操作中完成它,但是通过管道来表达它可以使代码更易于阅读。首先,我们设置数据:

As an example, imagine having a DataFrame with columns for stores, products, revenue and quantity sold. We’d like to do a groupwise calculation of prices (i.e. revenue/quantity) per store and per product. We could do this in a multi-step operation, but expressing it in terms of piping can make the code more readable. First we set the data:

In [265]: n = 1000

In [266]: df = pd.DataFrame(
   .....:     {
   .....:         "Store": np.random.choice(["Store_1", "Store_2"], n),
   .....:         "Product": np.random.choice(["Product_1", "Product_2"], n),
   .....:         "Revenue": (np.random.random(n) * 50 + 10).round(2),
   .....:         "Quantity": np.random.randint(1, 10, size=n),
   .....:     }
   .....: )
   .....:

In [267]: df.head(2)
Out[267]:
     Store    Product  Revenue  Quantity
0  Store_2  Product_1    26.12         1
1  Store_2  Product_1    28.86         1

现在,我们找到每商店/产品的价格。

We now find the prices per store/product.

In [268]: (
   .....:     df.groupby(["Store", "Product"])
   .....:     .pipe(lambda grp: grp.Revenue.sum() / grp.Quantity.sum())
   .....:     .unstack()
   .....:     .round(2)
   .....: )
   .....:
Out[268]:
Product  Product_1  Product_2
Store
Store_1       6.82       7.05
Store_2       6.30       6.64

当你希望传递一个分组对象给一些任意函数时,管道也可以表达出来,例如:

Piping can also be expressive when you want to deliver a grouped object to some arbitrary function, for example:

In [269]: def mean(groupby):
   .....:     return groupby.mean()
   .....:

In [270]: df.groupby(["Store", "Product"]).pipe(mean)
Out[270]:
                     Revenue  Quantity
Store   Product
Store_1 Product_1  34.622727  5.075758
        Product_2  35.482815  5.029630
Store_2 Product_1  32.972837  5.237589
        Product_2  34.684360  5.224000

这里,mean 取一个 GroupBy 对象,分别找到每个商店-产品组合的收入和数量列的平均值。mean 函数可以是任何接收 GroupBy 对象的函数;.pipe 将把 GroupBy 对象作为一个参数传递到你指定的函数中。

Here mean takes a GroupBy object and finds the mean of the Revenue and Quantity columns respectively for each Store-Product combination. The mean function can be any function that takes in a GroupBy object; the .pipe will pass the GroupBy object as a parameter into the function you specify.

Examples

Multi-column factorization

通过使用 DataFrameGroupBy.ngroup(),我们可以提取有关组的信息,类似于 factorize()(如 reshaping API 中进一步描述),但是它自然适用于混合类型和不同来源的多个列。它可以在处理中用作一个类似于分类的中间步骤,此时,组行之间的关系比其内容更重要,或者作为仅接受整数编码的算法的输入。(有关对 pandas 内完整分类数据的支持的更多信息,请参阅 Categorical introductionAPI documentation。)

By using DataFrameGroupBy.ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows are more important than their content, or as input to an algorithm which only accepts the integer encoding. (For more information about support in pandas for full categorical data, see the Categorical introduction and the API documentation.)

In [271]: dfg = pd.DataFrame({"A": [1, 1, 2, 3, 2], "B": list("aaaba")})

In [272]: dfg
Out[272]:
   A  B
0  1  a
1  1  a
2  2  a
3  3  b
4  2  a

In [273]: dfg.groupby(["A", "B"]).ngroup()
Out[273]:
0    0
1    0
2    1
3    2
4    1
dtype: int64

In [274]: dfg.groupby(["A", [0, 0, 0, 1, 1]]).ngroup()
Out[274]:
0    0
1    0
2    1
3    3
4    2
dtype: int64

Groupby by indexer to ‘resample’ data

重新采样从已经存在的观察数据或生成数据的模型中产生新的假设样本(重新采样)。这些新样本类似于先前存在的样本。

Resampling produces new hypothetical samples (resamples) from already existing observed data or from a model that generates data. These new samples are similar to the pre-existing samples.

为了使重新采样能够对非时间片段的索引工作,可以使用以下步骤。

In order for resample to work on indices that are non-datetimelike, the following procedure can be utilized.

在以下示例中,df.index // 5 返回一个整数数组,用于确定在 groupby 操作中选择什么。

In the following examples, df.index // 5 returns an integer array which is used to determine what gets selected for the groupby operation.

下面的示例显示了我们如何通过将样本合并为较少的样本来进行降采样。这里通过使用 df.index // 5,我们正在将样本聚合到垃圾箱中。通过应用 std() 函数,我们将包含在许多样本中的信息聚合到一小部分的值中,这些值是其标准偏差,从而减少了样本的数量。

The example below shows how we can downsample by consolidation of samples into fewer ones. Here by using df.index // 5, we are aggregating the samples in bins. By applying std() function, we aggregate the information contained in many samples into a small subset of values which is their standard deviation thereby reducing the number of samples.

In [275]: df = pd.DataFrame(np.random.randn(10, 2))

In [276]: df
Out[276]:
          0         1
0 -0.793893  0.321153
1  0.342250  1.618906
2 -0.975807  1.918201
3 -0.810847 -1.405919
4 -1.977759  0.461659
5  0.730057 -1.316938
6 -0.751328  0.528290
7 -0.257759 -1.081009
8  0.505895 -1.701948
9 -1.006349  0.020208

In [277]: df.index // 5
Out[277]: Index([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype='int64')

In [278]: df.groupby(df.index // 5).std()
Out[278]:
          0         1
0  0.823647  1.312912
1  0.760109  0.942941

Returning a Series to propagate names

对 DataFrame 列进行分组,计算一组度量并返回一个命名的 Series。该 Series 名称用作列索引名称。这在与堆叠等重塑操作结合使用时特别有用,其中列索引名称将用作插入列的名称:

Group DataFrame columns, compute a set of metrics and return a named Series. The Series name is used as the name for the column index. This is especially useful in conjunction with reshaping operations such as stacking, in which the column index name will be used as the name of the inserted column:

In [279]: df = pd.DataFrame(
   .....:     {
   .....:         "a": [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
   .....:         "b": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
   .....:         "c": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
   .....:         "d": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
   .....:     }
   .....: )
   .....:

In [280]: def compute_metrics(x):
   .....:     result = {"b_sum": x["b"].sum(), "c_mean": x["c"].mean()}
   .....:     return pd.Series(result, name="metrics")
   .....:

In [281]: result = df.groupby("a").apply(compute_metrics, include_groups=False)

In [282]: result
Out[282]:
metrics  b_sum  c_mean
a
0          2.0     0.5
1          2.0     0.5
2          2.0     0.5

In [283]: result.stack(future_stack=True)
Out[283]:
a  metrics
0  b_sum      2.0
   c_mean     0.5
1  b_sum      2.0
   c_mean     0.5
2  b_sum      2.0
   c_mean     0.5
dtype: float64