Skip to main contentSkip to solution

Which of the following are the valid ways to calculate the average marks in 'Maths'?

(A) df['Maths'].mean()

(B) df.loc[:,'Maths'].mean()

(C) df.query('Maths').mean()

(D) df['Maths'].aggregate('mean')

Solution

✅ Correct Option: 1

(A), (B) and (D) are valid. Each one first selects the 'Maths' column as a Series and then computes its mean.

df['Maths'].mean() selects the column and calls mean().

df.loc[:,'Maths'].mean() selects all rows (:) of the 'Maths' column by label, giving the same Series, then calls mean().

df['Maths'].aggregate('mean') passes the function name as a string to aggregate(), which gives the same result as mean().


(C): query() filters rows using a boolean expression, for example df.query('Maths > 50'). It does not select a column, and 'Maths' alone is not a condition that filters rows. It does not return the Maths column, so it cannot give the average.

Keyboard Shortcuts

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