當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python cudf.core.groupby.groupby.GroupBy.agg用法及代碼示例

用法:

GroupBy.agg(func)

將聚合應用到組。

參數

funcstr,可調用,列表或字典

返回

包含組合結果的係列或數據幀
聚合。

例子

>>> import cudf
>>> a = cudf.DataFrame(
    {'a': [1, 1, 2], 'b': [1, 2, 3], 'c': [2, 2, 1]})
>>> a.groupby('a').agg('sum')
   b  c
a
2  3  1
1  3  4

指定要在每列上執行的聚合列表。

>>> a.groupby('a').agg(['sum', 'min'])
    b       c
  sum min sum min
a
2   3   3   1   1
1   3   1   4   2

使用 dict 指定要在每列中執行的聚合。

>>> a.groupby('a').agg({'a': 'max', 'b': ['min', 'mean']})
    a   b
  max min mean
a
2   2   3  3.0
1   1   1  1.5

使用 lambdas/callables 來指定帶參數的聚合。

>>> f1 = lambda x: x.quantile(0.5); f1.__name__ = "q0.5"
>>> f2 = lambda x: x.quantile(0.75); f2.__name__ = "q0.75"
>>> a.groupby('a').agg([f1, f2])
     b          c
  q0.5 q0.75 q0.5 q0.75
a
1  1.5  1.75  2.0   2.0
2  3.0  3.00  1.0   1.0

相關用法


注:本文由純淨天空篩選整理自rapids.ai大神的英文原創作品 cudf.core.groupby.groupby.GroupBy.agg。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。