0
0
Pandasdata~10 mins

GroupBy with pipe for chaining 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 group the DataFrame by 'Category'.

Pandas
grouped = df.[1]('Category')
Drag options to blanks, or click blank then click option'
Agroupby
Bfilter
Csort_values
Dmerge
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filter' instead of 'groupby'.
Trying to sort instead of group.
2fill in blank
medium

Complete the code to calculate the mean of each group.

Pandas
result = df.groupby('Category').[1]()
Drag options to blanks, or click blank then click option'
Amax
Bsum
Ccount
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' which adds values instead of averaging.
Using 'count' which counts rows.
3fill in blank
hard

Fix the error in the code to chain groupby and mean using pipe.

Pandas
result = df.[1](lambda x: x.groupby('Category').mean())
Drag options to blanks, or click blank then click option'
Aapply
Bpipe
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'apply' which works differently here.
Using 'map' which is for element-wise mapping.
4fill in blank
hard

Fill both blanks to create a dictionary of group sizes for groups with size greater than 2.

Pandas
sizes = df.groupby('Category').[1]().to_dict()
filtered = {k: v for k, v in sizes.items() if v [2] 2}
Drag options to blanks, or click blank then click option'
Asize
B>
C<
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' instead of 'size' which can differ.
Using '<' instead of '>' in filtering.
5fill in blank
hard

Fill all three blanks to create a DataFrame with mean sales per category, reset index, and rename the column to 'Average Sales'.

Pandas
result = (df.groupby([1]).[2]().reset_index().rename(columns=[3]))
Drag options to blanks, or click blank then click option'
A'Category'
Bmean
C{'Sales': 'Average Sales'}
D'Sales'
Attempts:
3 left
💡 Hint
Common Mistakes
Not resetting the index after groupby.
Renaming the wrong column name.
Using wrong column name in groupby.