P87PandasBeginnerImplement8 min

Implement Group-Level Normalization With Pandas Transform

Normalize values relative to each row's group while preserving the original DataFrame shape.

#implement#groupby#transform

Scenario

A sales table has one row per representative and needs each sale expressed relative to that representative's region average.

Problem statement

Implement the group-level normalization and explain why `transform` is appropriate.

Your Task

  1. Compute a region-level mean aligned to each row.
  2. Create the normalized value.
  3. Explain why aggregation alone is insufficient.

Input

df = pd.DataFrame({'region': ['East', 'East', 'West'], 'sales': [100, 140, 90]})

Related Concepts

Pandas groupbyTransformData grain

Ready to count this problem?

Mark it complete when you have reasoned through the solution in your own words.