Skip to main contentSkip to solution

Consider the following DataFrame 'df' where the sales made by the companies in 3 quarters, is given:

NameQuarterSedanHatchbackCoupeSUV
Ford150604080
Ford240506070
Ford345573364
Mahindra160705049
Mahindra240503060
Mahindra355604576
Honda148543966
Honda241583263
Honda359674279

Find the average sale made by each company in each quarter.

Solution

✅ Correct Option: 4

df.GROUPBY(['Quarter']).aggregate('mean') groups the rows by the Quarter column and then applies aggregate with the function name 'mean'. This gives the mean of every numeric column (Sedan, Hatchback, Coupe, SUV) for each quarter. It is the only option with a properly closed GROUPBY(...) call and aggregate called with an argument.


Options 1, 2 and 3: the bracket in GROUPBY(['Quarter'] is never closed with ), so the statement is a syntax error.

Options 1, 2 and 3: sum, aggregate and agg are methods and must be called with brackets. Written without them and followed by .mean(), they do not compute anything.

In real pandas the method name is written in lowercase, as groupby.

Keyboard Shortcuts

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