🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.transform()

AI & DATA SCIENCE // df-transform

df.transform() applies a function to grouped (or ungrouped) data and returns a result with the exact same shape and index as the original, unlike agg(), which collapses each group down to a single summary value.

Syntax

df.transform(func)

Deep Dive Course

Where a groupby().agg() call reduces each group down to one summary row, groupby().transform() instead broadcasts the computed result back out to match the original DataFrame's full shape and row count — every row in a group gets that group's computed value, like the group's mean, attached right alongside its own original value. This makes transform() the right tool for tasks like computing each row's deviation from its group's average, since it produces a result aligned row-for-row with the original data, ready to subtract or compare directly.

1Understanding df.transform()

Where a groupby().agg() call reduces each group down to one summary row, groupby().transform() instead broadcasts the computed result back out to match the original DataFrame's full shape and row count — every row in a group gets that group's computed value, like the group's mean, attached right alongside its own original value. This makes transform() the right tool for tasks like computing each row's deviation from its group's average, since it produces a result aligned row-for-row with the original data, ready to subtract or compare directly.

💡

Use groupby().transform() specifically when you need each row to keep its own individual value while also gaining a group-level computed value alongside it, like normalizing each value against its group's mean — agg() alone can't do this, since it collapses each group down to one row and loses that per-row alignment.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
df["team_avg"] = df.groupby("team")["score"].transform("mean")
print(df)
localhost:3000

2Practical Example

Here is a real-world application of df.transform() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
df["deviation"] = df["score"] - df.groupby("team")["score"].transform("mean")
print(df)
localhost:3000

3Best Practices

Follow these guidelines when working with df.transform():

1. Use transform() when the result needs to align row-for-row with the original DataFrame, unlike agg()'s group-collapsing behavior

2. Use transform() to compute group-relative values, like each row's deviation from its group's mean, in a way ready for direct subtraction from the original column

3. Use agg() instead when you actually want one summary row per group, not a value repeated across every row in that group

⚠️

Tip: Use groupby().transform() specifically when you need each row to keep its own individual value while also gaining a group-level computed value alongside it, like normalizing each value against its group's mean — agg() alone can't do this, since it collapses each group down to one row and loses that per-row alignment.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
df["team_avg"] = df.groupby("team")["score"].transform("mean")
print(df)
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
df["team_avg"] = df.groupby("team")["score"].transform("mean")
print(df)
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
df["deviation"] = df["score"] - df.groupby("team")["score"].transform("mean")
print(df)

Best Practices

  • Use transform() when the result needs to align row-for-row with the original DataFrame, unlike agg()'s group-collapsing behavior
  • Use transform() to compute group-relative values, like each row's deviation from its group's mean, in a way ready for direct subtraction from the original column
  • Use agg() instead when you actually want one summary row per group, not a value repeated across every row in that group

Interview Question

Why does grouping by team and calling transform('mean') return a result with the same number of rows as the original DataFrame, while grouping by team and calling mean() directly returns only one row per group?

Hint: Think about what transform() is specifically designed to preserve, compared to agg()-style methods.

agg()-style aggregations like a plain groupby().mean() are designed to collapse each group down to a single summary row, reducing the total row count to one per group. transform() is specifically designed to do the opposite: it still computes the same group-level statistic internally, but then broadcasts that single group value back out to every original row that belonged to that group, preserving the original DataFrame's full row count and index alignment, so the group's mean shows up repeated alongside each individual row's own original value rather than replacing the group with one summary row.

Exercises

MediumPractice using df.transform() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
df["team_avg"] = df.groupby("team")["score"].transform("mean")
print(df)

Frequently Asked Questions

Why does grouping by team and calling transform('mean') return a result with the same number of rows as the original DataFrame, while grouping by team and calling mean() directly returns only one row per group?

agg()-style aggregations like a plain groupby().mean() are designed to collapse each group down to a single summary row, reducing the total row count to one per group. transform() is specifically designed to do the opposite: it still computes the same group-level statistic internally, but then broadcasts that single group value back out to every original row that belonged to that group, preserving the original DataFrame's full row count and index alignment, so the group's mean shows up repeated alongside each individual row's own original value rather than replacing the group with one summary row.

Related Functions

df-groupbydf-aggdf-apply