1. Home
  2. Data Science
  3. Pandas
  4. df.pivot_table(index='dept', columns='month'…

Added 30 Nov -0001

df.pivot_table(index='dept', columns='month', values='sales') is called without aggfunc. What happens when several rows share a dept and month?

  1. The duplicate cells are averaged, because the default aggregation is the mean Correct answer
  2. A ValueError is raised, because duplicate entries need an explicit aggfunc
  3. The last row for each cell wins, because pivot_table keeps the final value
  4. The rows are concatenated into a list inside the cell, preserving every value

Correct answer

A. The duplicate cells are averaged, because the default aggregation is the mean

12 views

MCQSEXAM

Explanation

pivot_table defaults to aggfunc='mean', so repeated index-column pairs are averaged instead of raising the way DataFrame.pivot does on duplicates. Pass aggfunc='sum', or a list such as ['sum','count'], when totals are wanted. Forgetting the default silently reports averages in columns that look like counts.

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