0
0
Pandasdata~10 mins

GroupBy with transform for normalization in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the mean of the 'score' column grouped by 'team'.

Pandas
mean_scores = df.groupby('team')['score'].[1]()
Drag options to blanks, or click blank then click option'
Asum
Bmax
Ccount
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() instead of mean()
Using count() which counts entries, not averages
2fill in blank
medium

Complete the code to normalize the 'score' column by subtracting the group mean using transform.

Pandas
df['score_normalized'] = df['score'] - df.groupby('team')['score'].[1]
Drag options to blanks, or click blank then click option'
Amean
Btransform('mean')
Csum
Dtransform('sum')
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean() instead of transform('mean') which returns a reduced Series
Using sum() which sums values instead of averaging
3fill in blank
hard

Fix the error in the code to correctly normalize 'score' by group using transform.

Pandas
df['score_norm'] = df.groupby('team')['score'].[1]('mean')
Drag options to blanks, or click blank then click option'
Atransform
Bagg
Capply
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean('mean') which is invalid
Using apply which may change the shape of the result
4fill in blank
hard

Fill both blanks to create a normalized score by subtracting the group mean and dividing by the group standard deviation.

Pandas
df['score_norm'] = (df['score'] - df.groupby('team')['score'].[1]) / df.groupby('team')['score'].[2]
Drag options to blanks, or click blank then click option'
Atransform('mean')
Bmean
Ctransform('std')
Dstd
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean and std without transform, which returns reduced Series
Mixing transform with non-transform methods
5fill in blank
hard

Fill all three blanks to create a new column with z-score normalization of 'score' grouped by 'team' using transform.

Pandas
df['score_z'] = (df[[1]] - df.groupby([2])[[3]].transform('mean')) / df.groupby([2])[[3]].transform('std')
Drag options to blanks, or click blank then click option'
A'score'
B'team'
Cscore
Dteam
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes causing KeyError
Mixing string and non-string column names