1. Home
  2. Data Science
  3. Pandas
  4. A categorical column with 20 categories is grouped, but…

Added 30 Nov -0001

A categorical column with 20 categories is grouped, but only 3 categories occur in the data. Why does observed=True shrink the result?

  1. Because observed=True drops rows whose category does not appear among the group keys chosen
  2. Because grouping a categorical converts it to an object column, losing category metadata
  3. Because observed=True sorts the categories and then truncates the output index
  4. Because the default expands every category combination, producing empty groups too Correct answer

Correct answer

D. Because the default expands every category combination, producing empty groups too

14 views

MCQSEXAM

Explanation

Categorical groupby keeps the full category set by default, so unobserved categories appear as empty groups, and with two categorical keys the result becomes their cartesian product. Passing observed=True restricts the result to combinations actually present, saving memory and time. pandas 3.0 changes the default to True, so code relying on the old behaviour may need updating.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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