- API reference
- DataFrame
- pandas.DataFrame.agg
pandas.DataFrame.agg#
- DataFrame.agg(func=None,axis=0,*args,**kwargs)[source]#
Aggregate using one or more operations over the specified axis.
- Parameters:
- funcfunction, str, list or dict
Function to use for aggregating the data. If a function, must eitherwork when passed a DataFrame or when passed to DataFrame.apply.
Accepted combinations are:
function
string function name
list of functions and/or function names, e.g.
[np.sum,'mean']
dict of axis labels -> functions, function names or list of such.
- axis{0 or ‘index’, 1 or ‘columns’}, default 0
If 0 or ‘index’: apply function to each column.If 1 or ‘columns’: apply function to each row.
- *args
Positional arguments to pass tofunc.
- **kwargs
Keyword arguments to pass tofunc.
- Returns:
- scalar, Series or DataFrame
The return can be:
scalar : when Series.agg is called with single function
Series : when DataFrame.agg is called with a single function
DataFrame : when DataFrame.agg is called with several functions
See also
DataFrame.apply
Perform any type of operations.
DataFrame.transform
Perform transformation type operations.
pandas.DataFrame.groupby
Perform operations over groups.
pandas.DataFrame.resample
Perform operations over resampled bins.
pandas.DataFrame.rolling
Perform operations over rolling window.
pandas.DataFrame.expanding
Perform operations over expanding window.
pandas.core.window.ewm.ExponentialMovingWindow
Perform operation over exponential weighted window.
Notes
The aggregation operations are always performed over an axis, either theindex (default) or the column axis. This behavior is different fromnumpy aggregation functions (mean,median,prod,sum,std,var), where the default is to compute the aggregation of the flattenedarray, e.g.,
numpy.mean(arr_2d)
as opposed tonumpy.mean(arr_2d,axis=0)
.agg is an alias foraggregate. Use the alias.
Functions that mutate the passed object can produce unexpectedbehavior or errors and are not supported. SeeMutating with User Defined Function (UDF) methodsfor more details.
A passed user-defined-function will be passed a Series for evaluation.
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 Csum 12.0 15.0 18.0min 1.0 2.0 3.0
Different aggregations per column.
>>>df.agg({'A':['sum','min'],'B':['min','max']}) A Bsum 12.0 NaNmin 1.0 2.0max NaN 8.0
Aggregate different functions over the columns and rename the index of the resultingDataFrame.
>>>df.agg(x=('A','max'),y=('B','min'),z=('C','mean')) A B Cx 7.0 NaN NaNy NaN 2.0 NaNz NaN NaN 6.0
Aggregate over the columns.
>>>df.agg("mean",axis="columns")0 2.01 5.02 8.03 NaNdtype: float64