Skip to main contentSkip to solution

Which of the following syntax are correct for data aggregation methods in pandas?

(A) df.aggregate('max')

(B) df.aggregate('sort')

(C) df['maths'].aggregate(['max', 'min'])

(D) df[['maths', 'science']].aggregate('sum', axis=1)

Solution

✅ Correct Option: 2

(A), (C) and (D) are valid. aggregate() (or agg()) accepts a function name as a string, a function, or a list of functions, and the function must reduce data to a single value.

(A) df.aggregate('max') gives the maximum of each column.

(C) df['maths'].aggregate(['max', 'min']) gives both the maximum and minimum of the Series.

(D) df[['maths', 'science']].aggregate('sum', axis=1) gives the row-wise sum of the two columns.


(B): 'sort' is not an aggregation function. It reorders data instead of reducing it, and pandas raises an error saying 'sort' is not a valid function for a DataFrame.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question