1. Home
  2. Data Science
  3. Pandas
  4. A wide table with one column per month must become long…

Added 30 Nov -0001

A wide table with one column per month must become long, with month and value columns. Which call does that?

  1. df.transpose, which swaps rows and columns and therefore creates a long form table
  2. df.melt with id_vars, which turns the monthly columns into rows of key-value pairs Correct answer
  3. df.pivot with the month columns as index, which expands the columns into extra rows
  4. df.stack on axis=1, which returns a wide frame with the months kept as columns

Correct answer

B. df.melt with id_vars, which turns the monthly columns into rows of key-value pairs

14 views

MCQSEXAM

Explanation

melt reshapes wide to long: id_vars stay fixed while value_vars become a variable column and a value column, so twelve monthly columns collapse into twelve rows per identifier. pivot performs the reverse, long to wide. stack is the MultiIndex equivalent of melt on columns, and unstack reverses it.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

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