Dataframe.aggregate() function is used to perform aggregation using one or more operation along the desired axis.
It uses callable, string, dict, or list of string/callables. It can take a string, a function, or a list thereof, and compute all the aggregates at once.
Syntax
DataFrame.aggregate(func, axis=0, *args, **kwargs)
Parameter
- func: It is a callable function, string, dict or list. It is used for aggression of data.
- axis:By Default 0.{0 or ‘index’, 1 or ‘columns’}
- *args: It is a Positional arguments to pass to function.
- **kwargs: It is a keyword arguments to pass to a function.
Return
It returns aggregated DataFrame.
Example
import pandas as pd
import numpy as np
Physics_marks=[44,np.NaN,47,28,39]
Chemistry_marks=[45,46,np.NaN,40,30]
Maths_marks=[35,38,29,30,np.NaN]
Students_marks=pd.DataFrame({'Physics':Physics_marks,
'Chemistry':Chemistry_marks,
'Maths':Maths_marks})
Students_marks
Output
Physics | Chemistry | Maths | |
---|---|---|---|
0 | 44.0 | 45.0 | 35.0 |
1 | NaN | 46.0 | 38.0 |
2 | 47.0 | NaN | 29.0 |
3 | 28.0 | 40.0 | 30.0 |
4 | 39.0 | 30.0 | NaN |
1. Along Rows
Students_marks.aggregate(['sum','max'], axis=0)
Output
Physics | Chemistry | Maths | |
---|---|---|---|
sum | 158.0 | 161.0 | 132.0 |
max | 47.0 | 46.0 | 38.0 |
2. Along Columns
Students_marks.aggregate(['sum','max'], axis=1)
Output
sum | max | |
---|---|---|
0 | 124.0 | 45.0 |
1 | 84.0 | 46.0 |
2 | 76.0 | 47.0 |
3 | 98.0 | 40.0 |
4 | 69.0 | 39.0 |