Skip to content

DOC: per docs, "margins" parameter returns row and column sums if True, but I think it returns the mean  #48916

Closed
@bhagerty

Description

@bhagerty

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/reference/api/pandas.pivot_table.html

Documentation problem

The documentation for the margins parameter says:

  • Add all row / columns (e.g. for subtotal / grand totals).

From what I can see, margins=True will return row and column means, not sums, at least if no aggfunc is specified. If there's a way to get sums, not means, I don't know what it is. Here's an example showing margins=True returning row and column means, not sums:

print(sales.pivot_table(values="weekly_sales", index="department", columns="type", fill_value=0, margins=True))
type                A           B        All
department                                  
1           30961.725   44050.627  32052.467
2           67600.159  112958.527  71380.023
...               ...         ...        ...
98          12875.423     217.428  11820.590
99            379.124       0.000    379.124
All         23674.667   25696.678  23843.950

Suggested fix for documentation

I don't know the fix for sure, because I don't know if margins=True behaves differently when an aggfunc is specified. But if it returns the mean rather than the sum, I suggest:

  • Provide the mean value for each row and each column.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions