Skip to content

Pivot

Base classes

Name Children Inherits
CallOp
datacook.engine.ops.base
An op that is one pandas call.

⋔ Inheritance diagram

graph TD
  3548707248["reshape.Pivot"]
  3484306131["base.CallOp"]
  1539482213["base.Op"]
  2751680898["builtins.object"]
  3484306131 --> 3548707248
  1539482213 --> 3484306131
  2751680898 --> 1539482213

🛈 DocStrings

Bases: CallOp

df.pivot_table(...): one value column, one column whose values become columns.

Source code in src/datacook/engine/ops/reshape.py
class Pivot(base.CallOp):
    """`df.pivot_table(...)`: one value column, one column whose values become columns."""

    id = "pivot"
    title = _("Pivot table")
    description = _(
        "Aggregate a value column per row key and per value of another column (long to wide"
        " format), optionally with totals."
    )
    category = categories.RESHAPE
    icon = "mdi.table-pivot"
    method = "pivot_table"
    then = f".reset_index(){LABELS_TO_TEXT}"

    class Params(base.Params):
        index: list[str] = Field(
            title=N_("Row keys"), min_length=1, json_schema_extra={"widget": "columns"}
        )
        columns: str = Field(
            title=N_("Column with the new column names"), json_schema_extra={"widget": "column"}
        )
        values: str = Field(title=N_("Value column"), json_schema_extra={"widget": "column"})
        aggfunc: Aggregation = Field("mean", title=N_("Function"))
        fill_value: float | None = Field(None, title=N_("Value for empty cells"))
        margins: bool = Field(False, title=N_("Include totals"))
        margins_name: str = Field("All", title=N_("Totals label"))
        dropna: bool = Field(True, title=N_("Drop missing keys"))
        sort: bool = Field(True, title=N_("Sort"))

    @classmethod
    def finish(cls, params: Any, result: Any) -> Any:
        return result.reset_index().rename(columns=str)

:fa-brands-github: Show source on GitHub