After res = df.groupby(['year', 'month']).size(), which call removes the year level from the resulting index?
Correct answer
A. res.droplevel('year') returns a Series indexed by month only
11 views
A. res.droplevel('year') returns a Series indexed by month only
11 views
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.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is A? Ask here and get an explanation.