0
0
Data Analysis Pythondata~10 mins

transform() for group-level operations in Data Analysis Python - 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 'score' for each group using transform.

Data Analysis Python
df['mean_score'] = df.groupby('group')['score'].[1](lambda x: x.mean())
Drag options to blanks, or click blank then click option'
Atransform
Bapply
Caggregate
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply returns a reduced result, not aligned with original DataFrame.
Using aggregate returns a smaller DataFrame, not suitable for adding new columns.
2fill in blank
medium

Complete the code to create a new column with the z-score of 'score' within each group.

Data Analysis Python
df['z_score'] = df.groupby('group')['score'].[1](lambda x: (x - x.mean()) / x.std())
Drag options to blanks, or click blank then click option'
Aapply
Bfilter
Ctransform
Daggregate
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply returns a reduced result, not suitable for element-wise transformations.
Using filter removes rows, not suitable for creating new columns.
3fill in blank
hard

Fix the error in the code to correctly calculate the cumulative sum of 'score' within each group.

Data Analysis Python
df['cum_sum'] = df.groupby('group')['score'].[1]('cumsum')
Drag options to blanks, or click blank then click option'
Atransform
Bapply
Caggregate
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply with cumsum returns a Series but may not align properly for assignment.
Using aggregate returns a reduced DataFrame, not suitable for new columns.
4fill in blank
hard

Fill both blanks to create a new column with the rank of 'score' within each group in descending order.

Data Analysis Python
df['rank'] = df.groupby('group')['score'].[1](lambda x: x.rank(method='[2]', ascending=False))
Drag options to blanks, or click blank then click option'
Atransform
Bdense
Caggregate
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply returns a reduced result, not aligned with original DataFrame.
Using method='average' instead of 'dense' changes the ranking style.
5fill in blank
hard

Fill all three blanks to create a new column with the normalized 'score' within each group, scaled between 0 and 1.

Data Analysis Python
df['normalized'] = df.groupby('group')['score'].[1](lambda x: (x - x.[2]()) / (x.[3]() - x.[2]()))
Drag options to blanks, or click blank then click option'
Atransform
Bmin
Cmax
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply instead of transform returns a reduced result, not suitable for new columns.
Swapping min and max functions changes the normalization calculation.