Skip to main contentSkip to solution

Arrange the following steps for using Group by functions in Pandas:

(A) Import relevant libraries

(B) Apply the required function

(C) Split the data into groups by creating a Group by objects from the original DataFrame

(D) Combine results into a new DataFrame

Solution

✅ Correct Option: 2

(A) Import relevant libraries, because Pandas must be imported before it can be used.

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

(B) Apply the required function, such as sum(), mean() or count(), to each group.

(D) Combine the results into a new DataFrame.

Group by follows the split-apply-combine pattern: split (C), apply (B), combine (D).


Option 1: applying the function (B) before splitting (C) is wrong, since the groups must exist before a function can act on them.

Keyboard Shortcuts

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