transform()
noun
say it: “trans-FORM” /trænsˈfɔrm/
A pandas grouping method that works out one answer per group and copies it onto every row of that group, so it lines up with the table.
What does transform() do after a groupby in pandas?
transform() is a groupby() method that works out a summary for each group and hands it back on every row of that
group, so the result lines up with the original table. That makes each row's share of its own group one division.
import pandas as pd
orders = pd.DataFrame({"neighborhood": ["Elm Park", "Elm Park", "Riverside"], "total": [30.0, 10.0, 25.0]})
orders["area_total"] = orders.groupby("neighborhood")["total"].transform("sum")
print(orders["area_total"].tolist()) # [40.0, 40.0, 25.0]
In a sentence
- “transform() put each neighborhood's total beside every one of its orders.”
- “Dividing by the transform() column gave each order's share of its neighborhood.”
Level: Freelancer · More Python terms at this level
Updated