Pandas DataFrame.aggregate()
The main task of DataFrame.aggregate() function is to apply some aggregation to one or more column. Most frequently used aggregations are:
sum: It is used to return the sum of the values for the requested axis.
min: It is used to return the minimum of the values for the requested axis.
max: It is used to return the maximum values for the requested axis.
Syntax:
DataFrame.aggregate(func, axis=0, *args, **kwargs)
Parameters:
func: It refers callable, string, dictionary, or list of string/callables.
It is used for aggregating the data. For a function, it must either work when passed to a DataFrame or DataFrame.apply(). For a DataFrame, it can pass a dict, if the keys are the column names.
axis: (default 0): It refers to 0 or ‘index’, 1 or ‘columns’
0 or ‘index’: It is an apply function for each column.
1 or ‘columns’: It is an apply function for each row.
*args: It is a positional argument that is to be passed to func .
**kwargs: It is a keyword argument that is to be passed to the func .
Returns:
It returns the scalar, Series or DataFrame.
scalar: It is being used when Series.agg is called with the single function.
Series: It is being used when DataFrame.agg is called for the single function.
DataFrame: It is being used when DataFrame.agg is called for the several functions.
Example:
import pandas as pd import numpy as np info=pd.DataFrame([[1,5,7],[10,12,15],[18,21,24],[np.nan,np.nan,np.nan]],columns=['X','Y','Z']) info.agg(['sum','min'])
Output:
X Y Z
sum 29.0 38.0 46.0
min 1.0 5.0 7.0
Example2:
import pandas as pd import numpy as np info=pd.DataFrame([[1,5,7],[10,12,15],[18,21,24],[np.nan,np.nan,np.nan]],columns=['X','Y','Z']) df.agg({'A' : ['sum', 'min'], 'B' : ['min', 'max']})
Output:
X Y
max NaN 21.0
min 1.0 12.0
sum 29.0 NaN