Consider the following DataFrame 'df' where the sales made by the companies in 3 quarters, is given:
Name Quarter Sedan Hatchback Coupe SUV Ford 1 50 60 40 80 Ford 2 40 50 60 70 Ford 3 45 57 33 64 Mahindra 1 60 70 50 49 Mahindra 2 40 50 30 60 Mahindra 3 55 60 45 76 Honda 1 48 54 39 66 Honda 2 41 58 32 63 Honda 3 59 67 42 79
Find the average sale made by each company in each quarter.
Consider the following DataFrame 'df' where the sales made by the companies in 3 quarters, is given:
| Name | Quarter | Sedan | Hatchback | Coupe | SUV |
|---|---|---|---|---|---|
| Ford | 1 | 50 | 60 | 40 | 80 |
| Ford | 2 | 40 | 50 | 60 | 70 |
| Ford | 3 | 45 | 57 | 33 | 64 |
| Mahindra | 1 | 60 | 70 | 50 | 49 |
| Mahindra | 2 | 40 | 50 | 30 | 60 |
| Mahindra | 3 | 55 | 60 | 45 | 76 |
| Honda | 1 | 48 | 54 | 39 | 66 |
| Honda | 2 | 41 | 58 | 32 | 63 |
| Honda | 3 | 59 | 67 | 42 | 79 |
Find the average sale made by each company in each quarter.
Solution
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.
Related questions:
2026: 18 May Shift 2
2025: 22 May Shift 1
2025: 20 May Shift 1
2026: 24 May Shift 1
2026: 21 May Shift 1