Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to group the DataFrame by 'Category'.
Pandas
grouped = df.[1]('Category')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filter' instead of 'groupby'.
Trying to sort instead of group.
✗ Incorrect
The groupby method groups data by a column, which is needed here.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' which adds values instead of averaging.
Using 'count' which counts rows.
✗ Incorrect
The mean() function calculates the average for each group.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'apply' which works differently here.
Using 'map' which is for element-wise mapping.
✗ Incorrect
The pipe method allows chaining custom functions like grouping and averaging.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' instead of 'size' which can differ.
Using '<' instead of '>' in filtering.
✗ Incorrect
size() gives group sizes, and filtering with > keeps groups larger than 2.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not resetting the index after groupby.
Renaming the wrong column name.
Using wrong column name in groupby.
✗ Incorrect
Group by 'Category', calculate mean, then rename 'Sales' column to 'Average Sales'.