What does df.groupby("city")["sales"].sum() compute in pandas?
Correct answer
B. Total sales per distinct city value
14 views
B. Total sales per distinct city value
14 views
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.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is B? Ask here and get an explanation.