1. Home
  2. Data Science
  3. Pandas
  4. What does df.groupby("city")["sales"].sum() compute in…

Added 30 Nov -0001

What does df.groupby("city")["sales"].sum() compute in pandas?

  1. Sales summed across all columns grouped by city
  2. Total sales per distinct city value Correct answer
  3. Overall sales total ignoring the city column
  4. The row count in each city group

Correct answer

B. Total sales per distinct city value

14 views

MCQSEXAM

Explanation

groupby splits rows by the key column, the column selection restricts the operation to sales, and sum aggregates within each group, so the result is a Series indexed by city. The split-apply-combine pattern is the same idea as SQL's GROUP BY with SUM. Swapping in .agg(["sum", "mean"]) returns several statistics at once and yields a DataFrame with a column MultiIndex.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Pandas MCQs

Practise all Pandas MCQs

Similar MCQs