Skip to main contentSkip to solution

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

✅ Correct Option: 1

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'}).

Keyboard Shortcuts

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