Skip to main contentSkip to solution

Arrange the following steps to use the GROUP BY() function in Pandas:

(A) Apply the required function.

(B) Import pandas.

(C) Split the data into groups by creating a GROUP BY object from the original DataFrame.

(D) Combine the results to form a new DataFrame.

Solution

✅ Correct Option: 2

Pandas must be imported before any of its functions can be used. After that, groupby follows the split-apply-combine pattern: split the data into groups, apply a function to each group, then combine the results into a new DataFrame.

(B) Import pandas.

(C) Split the data into groups by creating a GroupBy object from the original DataFrame.

(A) Apply the required function (sum, mean, count and so on) to each group.

(D) Combine the results to form a new DataFrame.


Option 1: applies the function before the groups exist, so there is nothing to apply it to.

Options 3 and 4: combine before applying, but the results to combine only exist after the function has been applied.

Keyboard Shortcuts

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