ENH: Ability to specify only `rows` or `columns` as `margins` using `pd.pivot_table`

This issue has been created since 2022-09-21.

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Currently, using pd.pivot_table(..., margins=True) applies aggfunc to both axes. This is not always useful, depending on the use case, and so I propose changing margins to allow the specific axis of interest to be specified.

e.g., in this example I am only interested in adding a daily total sales

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "state": ["CA", "WA", "CO", "AZ"] * 3,
        "office_id": list(range(1, 7)) * 2,
        "date": [pd.Timestamp.now().date() - pd.Timedelta(days=d) for d in range(6)] * 2,
        "sales": [np.random.randint(100_000, 999_999) for _ in range(12)]
    }
).astype(
    {
        "sales": np.float64,
        "date": pd.api.types.pandas_dtype("O")
    }
)

df.pivot_table(index=["office_id", "date"], columns="state", margins=True, aggfunc="sum").iloc[:-1, :].stack() # workaround
df.pivot_table(index=["office_id", "date"], columns="state", margins="rows", aggfunc="sum").stack() # proposed syntax

Feature Description

from typing import Literal


def pivot_table(
    data: DataFrame,
    ...,
    margins: Literal["rows", "columns", "both"] | bool = False

Perhaps in future, boolean argument would be depreciated in favour of more explicit "both" argument.

Alternative Solutions

Currently the only way to achieve this outcome is by slicing to remove the axis which is not of interest (i.e. in above example with iloc)

Additional Context

No response

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-09-29
Star Count 35373
Watcher Count 1123
Fork Count 15031
Issue Count 3581

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Adapting to large database schema 2 2022-02-09 2022-09-25
When resizing a flipped NinePatch the drag controls are also inverted 0 2021-10-20 2022-09-03
plot_network_clusters() got an unexpected keyword argument 'top_k' 3 2021-03-02 2022-05-15
Avoid writing registry URL to git 1 2022-08-01 2022-09-18
FLASH sector description is wrong for NUCLEO F722ZE 3 2021-10-27 2022-09-15
[QUERY] Azure function upgrade from .NET Core 3.1 to .NET 6 for service bus triggered function 1 2022-04-25 2022-09-21
4.3.0 release is giving an error 2 2022-08-22 2022-09-06
Cannot build the file correctly 2 2021-12-16 2022-09-28
DM: TestMaster is unstable. 0 2022-01-13 2022-07-18
Int columns get converted to String on realtime listening (for one row) 8 2021-12-01 2022-09-20
[Question]Doesn't torch tensorrt support LSTM-based decoder optimization?? 2 2022-04-27 2022-09-13
Replace fallback locale configuration and logic by dedicated Strategy class 2 2022-01-14 2022-09-09
设计朋友圈时间线功能 :: labuladong的算法小抄 3 2021-08-01 2022-01-12
NoSuchMethodError: java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer; 3 2021-05-24 2022-09-04
Data filtering feature 1 2021-05-02 2022-08-27
Ukrainian, not Ukranian 2 2022-04-11 2022-09-10
symbol lookup error on nVidia 13 2016-05-27 2022-07-25
Bug in "destroy" metod of localStorage.js 2 2018-02-02 2022-09-09
nlohmann::to_json method not acting as expected with nlohmann::adl_serializer specialization. 3 2022-02-18 2022-09-27
Export types 2 2021-11-16 2022-01-07
Session variables are leaking across migration scripts 5 2021-05-26 2022-09-28
[IDEA] ZModem support receive/send files using lrzsz 0 2021-02-03 2022-09-11
Fix test harness to be less fiddly 1 2020-05-04 2022-08-25
Date Timezone Issue For Tasks Reports 1 2021-08-25 2022-09-07
Rate limiters can orphan connections when preloading app code 2 2022-09-20 2022-09-28
How to backup? 0 2021-09-07 2022-01-07
How to install the cli on Ubuntu? 1 2021-10-26 2022-07-31
Is there any way to extended the Create-Call Callback with custom data? 2 2022-04-13 2022-09-03
java.lang.NoSuchMethodError: 'long net.minecraft.SystemUtils.b()' 4 2021-12-18 2022-09-25
ERR! ERR_TLS_CERT_ALTNAME_INVALID 1 2021-10-01 2022-09-12
Lift `'static` requirement on resource `attr` fields 0 2021-07-12 2022-08-13
get_feature_names() FutureWarning 5 2021-12-02 2022-08-06
Support multi-master database setup 11 2018-10-02 2022-08-21
clang-tidy 15.0.0: readability-simplify-boolean-expr 2 2022-09-19 2022-09-21
Crash at -O1 and above on valid code: Assertion `(!IsSigned || !isKnownNonPositive(Stride)) && "Stride is expected strictly positive for signed case!"' failed. 0 2022-09-19 2022-09-23
Discovery of Microsoft.Authorization/policyExemptions 0 2021-07-28 2022-09-29
hash/fnv has collision cause the wait container process not as expected when create workflow as same name 5 2022-07-08 2022-08-10
TAR extractcontents stuck in loop 6 2020-10-08 2022-09-25
PagerDutyNotificationPlugin URL is incorrect, and sample code error 2 2017-09-05 2022-08-21
dimens giving error: Cannot resolve symbol '@dimen/_12sdp' less... (Ctrl+F1) Inspection info: Validates resource references inside Android XML files. 2 2019-03-06 2022-09-16
Automatically tree shake objects from schema only referenced in hidden queries 1 2021-11-10 2022-09-01
Allow multiple metrics to be displayed in a single diagram 2 2021-10-28 2022-09-28
fix for flake8-isort - [merged] 4 2021-04-04 2022-09-20
The configuration synchronization service is abnormal 1 2022-07-15 2022-08-27
Getting better results than the reported ones 5 2018-10-05 2022-08-11
Follow dark style preference 1 2022-06-04 2022-08-03
When signaling to a waiting task, do not signal under the hold of a l… 1 2019-12-02 2022-01-25
Don't log exception in exception handling middleware 1 2022-07-27 2022-09-14
Setting SlidingExpiration on a CacheEntry has some seemingly undocumented behavior 2 2022-03-10 2022-03-06
.NET Portability Analyzer does not install in VS 2022 3 2022-03-24 2022-07-22