0
0
Pandasdata~10 mins

transform() for group-level operations 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 each group using transform.

Pandas
df['group_mean'] = df.groupby('group')['value'].[1](lambda x: x.mean())
Drag options to blanks, or click blank then click option'
Atransform
Bfilter
Cagg
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply() returns a reduced result, not matching the original DataFrame shape.
Using agg() returns aggregated results, not suitable for adding new columns with original shape.
2fill in blank
medium

Complete the code to subtract the group mean from each value using transform.

Pandas
df['value_centered'] = df['value'] - df.groupby('group')['value'].[1](lambda x: x.mean())
Drag options to blanks, or click blank then click option'
Afilter
Btransform
Capply
Dagg
Attempts:
3 left
💡 Hint
Common Mistakes
Using agg() returns a smaller series, causing alignment errors.
Using apply() may return a series with different shape.
3fill in blank
hard

Fix the error in the code to correctly calculate the z-score within each group.

Pandas
df['z_score'] = df.groupby('group')['value'].[1](lambda x: (x - x.mean()) / x.std())
Drag options to blanks, or click blank then click option'
Aapply
Bagg
Ctransform
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using agg() causes shape mismatch errors.
Using apply() may not preserve the original index alignment.
4fill in blank
hard

Fill both blanks to create a new column with the max value per group and then subtract the min value per group.

Pandas
df['range'] = df.groupby('group')['value'].[1]('max') - df.groupby('group')['value'].[2]('min')
Drag options to blanks, or click blank then click option'
Atransform
Bapply
Cagg
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using agg() returns aggregated results that cannot be subtracted element-wise.
Using apply() may cause misalignment.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

Pandas
lengths = {word: {BLANK_2}} for word in words if {{BLANK_2}}
Drag options to blanks, or click blank then click option'
A:
Blen(word)
Clen(word) > 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the colon between key and value.
Using the wrong condition or value expression.