Pandas DataFrame Aggregate: agg() function

Profile picture for user devanshi.srivastava
Submitted by devanshi.srivastava on

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

 PhysicsChemistryMaths
044.045.035.0
1NaN46.038.0
247.0NaN29.0
328.040.030.0
439.030.0NaN

1. Along Rows

Students_marks.aggregate(['sum','max'], axis=0)

Output

 PhysicsChemistryMaths
sum158.0161.0132.0
max47.046.038.0

2. Along Columns

Students_marks.aggregate(['sum','max'], axis=1)

Output

 summax
0124.045.0
184.046.0
276.047.0
398.040.0
469.039.0