pandas.DataFrame.aggregate¶
-
DataFrame.
aggregate
(func, axis=0, *args, **kwargs)[source]¶ Aggregate using one or more operations over the specified axis.
New in version 0.20.0.
Parameters: func : function, string, dictionary, or list of string/functions
Function to use for aggregating the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply. For a DataFrame, can pass a dict, if the keys are DataFrame column names.
Accepted combinations are:
- string function name.
- function.
- list of functions.
- dict of column names -> functions (or list of functions).
axis : {0 or ‘index’, 1 or ‘columns’}, default 0
- 0 or ‘index’: apply function to each column.
- 1 or ‘columns’: apply function to each row.
*args
Positional arguments to pass to func.
**kwargs
Keyword arguments to pass to func.
Returns: - aggregated : DataFrame
See also
DataFrame.apply
- Perform any type of operations.
DataFrame.transform
- Perform transformation type operations.
pandas.core.groupby.GroupBy
- Perform operations over groups.
pandas.core.resample.Resampler
- Perform operations over resampled bins.
pandas.core.window.Rolling
- Perform operations over rolling window.
pandas.core.window.Expanding
- Perform operations over expanding window.
pandas.core.window.EWM
- Perform operation over exponential weighted window.
Notes
agg is an alias for aggregate. Use the alias.
A passed user-defined-function will be passed a Series for evaluation.
The aggregation operations are always performed over an axis, either the index (default) or the column axis. This behavior is different from numpy aggregation functions (mean, median, prod, sum, std, var), where the default is to compute the aggregation of the flattened array, e.g.,
numpy.mean(arr_2d)
as opposed tonumpy.mean(arr_2d, axis=0)
.agg is an alias for aggregate. Use the alias.
Examples
>>> df = pd.DataFrame([[1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... [np.nan, np.nan, np.nan]], ... columns=['A', 'B', 'C'])
Aggregate these functions over the rows.
>>> df.agg(['sum', 'min']) A B C sum 12.0 15.0 18.0 min 1.0 2.0 3.0
Different aggregations per column.
>>> df.agg({'A' : ['sum', 'min'], 'B' : ['min', 'max']}) A B max NaN 8.0 min 1.0 2.0 sum 12.0 NaN
Aggregate over the columns.
>>> df.agg("mean", axis="columns") 0 2.0 1 5.0 2 8.0 3 NaN dtype: float64