pandas.DataFrame.aggregate() function with example in python

Spread the love

pandas.DataFrame.aggregate() or pandas.DataFrame.agg(): This function Aggregate using one or more operations over the specified axis.
Syntax: DataFrame.aggregate(func, axis=0, *args, **kwargs)

New in version 0.20.0.

Parameters:
func : function, str, list or dict
Function to use for aggregating the data. If a function, must either work 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 to func.

**kwargs
Keyword arguments to pass to func.

Returns:
DataFrame, Series or scalar
if DataFrame.agg is called with a single function, returns a Series if DataFrame.agg is called with several functions, returns a DataFrame if Series.agg is called with single function, returns a scalar if Series.agg is called with several functions, returns a Series

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 to “numpy.mean(arr_2d,
axis=0)“.
`agg` is an alias for `aggregate`. Use the alias.

Example Program:

import pandas as pd df = pd.DataFrame({'x': [1, 2, 2,3,4,5], 'y': [10,20,30,40,50,60]}) print(df.agg(['sum', 'min']))

Output:

 

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *