1. Home
  2. Data Science
  3. Pandas
  4. After res = df.groupby(['year', 'month']).size(), which…

Added 30 Nov -0001

After res = df.groupby(['year', 'month']).size(), which call removes the year level from the resulting index?

  1. res.droplevel('year') returns a Series indexed by month only Correct answer
  2. res.reset_index(level='year') removes the level without adding a column
  3. res.drop('year') drops the level from the index
  4. res.swaplevel() removes the level entirely

Correct answer

A. res.droplevel('year') returns a Series indexed by month only

11 views

MCQSEXAM

Explanation

MultiIndex level handling: droplevel discards a level and keeps the remaining one as the index, so the result is indexed by month alone. reset_index(level='year') instead promotes that level into a column, which is what you want when turning grouped output back into a flat table. swaplevel only exchanges level order, and unstack pivots a level into columns.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

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