0
0
Pandasdata~10 mins

GroupBy performance considerations 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 the 'category' column.

Pandas
grouped = df.groupby([1])
Drag options to blanks, or click blank then click option'
Adf['category']
B'category'
Ccategory
D'Category'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the column name without quotes causes a NameError.
Passing the whole Series instead of the column name string.
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'
Asum
Bmax
Ccount
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() instead of mean().
Using count() which counts entries, not averages.
3fill in blank
hard

Fix the error in this code to avoid slow performance when grouping large DataFrames.

Pandas
result = df.groupby('category')[[1]].mean()
Drag options to blanks, or click blank then click option'
A'value'
Bdf.columns
Cdf['value']
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Passing df['value'] instead of the column name string.
Passing df.columns which is a list of all columns.
4fill in blank
hard

Fill both blanks to create a dictionary of group sizes for groups with more than 5 entries.

Pandas
sizes = {group: len(data) for group, data in df.groupby([1]) if len(data) [2] 5}
Drag options to blanks, or click blank then click option'
A'category'
B>
C<
D'value'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column name for grouping.
Using '<' instead of '>' to filter group sizes.
5fill in blank
hard

Fill all three blanks to create a dictionary with group names in uppercase and sum of 'value' for groups with sum greater than 10.

Pandas
result = {group[1]: data['[2]'].sum() for group, data in df.groupby('[3]') if data['value'].sum() > 10}
Drag options to blanks, or click blank then click option'
A.upper()
Bvalue
Ccategory
D.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.lower()' instead of '.upper()' for group names.
Using wrong column names for sum or grouping.