Skip to content

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

Closed
@bollard

Description

@bollard

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team memberReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions