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)
|