A DataFrame 'df' has columns 'Mobile_brands' and 'unit_sold'. How will you calculate the total 'unit_sold' for each 'Mobile_brand' using groupby function?
A DataFrame 'df' has columns 'Mobile_brands' and 'unit_sold'. How will you calculate the total 'unit_sold' for each 'Mobile_brand' using groupby function?
Solution
df.groupby('Mobile_brand') splits the rows into one group per brand. Selecting ['unit_sold'] picks the column to total, and .sum() adds up its values within each group. The result is a Series indexed by brand, with the total units sold for each.
Options 2, 3 and 4: pandas has no total() method and no 'total' aggregation name, so these fail. They also have a missing closing quote after 'Mobile_brand, which is a syntax error.
The correct function for adding values is sum, as in .sum() or .agg({'unit_sold': 'sum'}).
Related questions:
2026: 31 May Shift 1
2026: 31 May Shift 1
2025: 15 May Shift 2
2026: 18 May Shift 2