1. Home
  2. Data Science
  3. Pandas
  4. What does df.xs(2021, level='year') return on a frame with…

Added 30 Nov -0001

What does df.xs(2021, level='year') return on a frame with a MultiIndex containing a year level?

  1. The frame sorted by the year level, keeping only the first matching rows
  2. The year level reset into an ordinary column of the same DataFrame
  3. A grouped object that aggregates the values for 2021 by the other index level
  4. A cross-section at one index level, with that level dropped from the result Correct answer

Correct answer

D. A cross-section at one index level, with that level dropped from the result

14 views

MCQSEXAM

Explanation

xs takes a cross-section of a MultiIndex: it selects every row whose year level equals 2021 and returns a frame with that level removed, so the result is indexed only by the remaining levels. Passing drop_level=False keeps the level in place. It is the readable alternative to a full .loc slice with slice(None) for the other levels.

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