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')
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
(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.
Related questions:
2026: 13 May Shift 2
2026: 23 May Shift 1