DataFrameGroupBy.
agg
(
arg,
*args,
**kwargs
)
[source]
Aggregate using callable, string, dict, or list of string/callables
| Parameters: | func : callable, string, dictionary, or list of string/callables
|
|---|---|
| Returns: | aggregated : DataFrame |
See also
pandas.DataFrame.groupby.apply, pandas.DataFrame.groupby.transform, pandas.DataFrame.aggregate
Notes
Numpy functions mean/median/prod/sum/std/var are special cased so the default behavior is applying the function along axis=0 (e.g., np.mean(arr_2d, axis=0)) as opposed to mimicking the default Numpy behavior (e.g., np.mean(arr_2d)).
agg is an alias for aggregate. Use the alias.
Examples
>>> df = pd.DataFrame({'A': [1, 1, 2, 2],
... 'B': [1, 2, 3, 4],
... 'C': np.random.randn(4)})
>>> df
A B C
0 1 1 0.362838
1 1 2 0.227877
2 2 3 1.267767
3 2 4 -0.562860
The aggregation is for each column.
>>> df.groupby('A').agg('min')
B C
A
1 1 0.227877
2 3 -0.562860
Multiple aggregations
>>> df.groupby('A').agg(['min', 'max'])
B C
min max min max
A
1 1 2 0.227877 0.362838
2 3 4 -0.562860 1.267767
Select a column for aggregation
>>> df.groupby('A').B.agg(['min', 'max'])
min max
A
1 1 2
2 3 4
Different aggregations per column
>>> df.groupby('A').agg({'B': ['min', 'max'], 'C': 'sum'})
B C
min max sum
A
1 1 2 0.590716
2 3 4 0.704907





本文介绍了 Pandas 库中 DataFrameGroupBy 类的 agg 方法的使用方式及参数说明。该方法允许用户通过字符串、函数、字典或函数列表进行聚合操作,支持多种输入组合形式,并展示了多个实例,包括单个聚合、多个聚合、选择列进行聚合以及不同列应用不同的聚合函数。
2203

被折叠的 条评论
为什么被折叠?



