Closed
Description
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.