What do the aggfunc and margins arguments of pandas pivot_table() do?

Quick Overview

Asks what the aggfunc and margins arguments control in a pandas pivot table and how the totals row and column are computed. It tests whether you know the default aggregation, the accepted forms of aggfunc, and why totals can disagree with the cells for averages.

What do the aggfunc and margins arguments of pandas pivot_table() do?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What do the `aggfunc` and `margins` arguments of `pivot_table()` do in pandas? ```hint Check the totals For the totals, ask which data each total is computed from. Then try it with an aggregation that is not additive and groups of uneven size. ``` ### What a Strong Answer Covers - What `aggfunc` does, its default, and the forms it accepts - How a list or a dict of functions changes the shape of the result - What `margins` adds and how its label is set - How the margin values are computed, and why that matters for functions such as the mean ### Follow-up Questions - How is `pivot_table` different from `pivot`, and when does `pivot` raise an error? - How would you build the same table with `groupby` and `unstack`? - How does `dropna` affect the margins when the data has missing values? - How would you show both the count and the mean of sales in each cell, with totals?

Overview: Asks what the aggfunc and margins arguments control in a pandas pivot table and how the totals row and column are computed. It tests whether you know the default aggregation, the accepted forms of aggfunc, and why totals can disagree with the cells for averages.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What do the aggfunc and margins arguments of pivot_table() do in pandas?

What a Strong Answer Covers Guidance

  • What aggfunc does, its default, and the forms it accepts
  • How a list or a dict of functions changes the shape of the result
  • What margins adds and how its label is set
  • How the margin values are computed, and why that matters for functions such as the mean

Follow-up Questions Guidance

  • How is pivot_table different from pivot , and when does pivot raise an error?
  • How would you build the same table with groupby and unstack ?
  • How does dropna affect the margins when the data has missing values?
  • How would you show both the count and the mean of sales in each cell, with totals?