Pandas GroupBy Transform
transform returns results aligned to the original rows rather than one reduced row per group. It is useful for adding a group total beside each observation.
How it works
A per-drop-off share divides one measurement by its point’s total. The denominator needs a zero and missing-value policy in general; the fixture has positive complete totals. Repeating a group total across rows does not make it additive again. If that repeated field is later summed, the report can multiply the same group fact several times.
Run the code
Select Try in playground to load the complete editable source. Nothing executes until you choose Run. The browser uses Pyodide 0.27.7, Pandas 2.2.3 and NumPy 2.0.2. Each ordinary execution starts a fresh isolated runtime. The tables are fictional and do not use personal files.
For local work, use a separate virtual environment and run python transform.py. Download the complete source. The reviewed local environment uses Python 3.12.14, Pandas 3.0.6 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with Pandas 2.2.3 and NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.
Example
import pandas as pd
import numpy as np
df = pd.DataFrame({
"drop_id": [101, 102, 103, 104],
"point": ["north", "south", "north", "south"],
"material": ["leaves", "food", "food", "leaves"],
"kg": [12., 8., 6., 10.],
"bags": pd.array([3, 2, 1, 2], dtype="Int64"),
"day": ["2026-10-01", "2026-10-01", "2026-10-02", "2026-10-02"],
})
totals = df.groupby("point", observed=True)["kg"].transform("sum")
shares = df["kg"] / totals
print(totals.tolist())
print(np.allclose(shares.groupby(df["point"]).sum().to_numpy(), [1., 1.]))
print(len(shares) == len(df))
Expected result
[18.0, 18.0, 18.0, 18.0] True True
The transform preserves four source rows and each point’s shares sum to one. The check uses the same group labels and a meaningful numerical tolerance rather than comparing rounded display strings.
Use Pandas’s official reference for precise contracts, supported versions and numerical limitations.