0
0
Pandasdata~10 mins

Named aggregation 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 'team' and calculate the mean of 'score' using named aggregation.

Pandas
result = df.groupby('team').agg({'average_score': ('score', [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' will add scores instead of averaging.
Using 'count' will count entries, not calculate average.
2fill in blank
medium

Complete the code to group by 'department' and calculate the maximum 'salary' with named aggregation.

Pandas
summary = df.groupby('department').agg({'max_salary': ('salary', [1])})
Drag options to blanks, or click blank then click option'
Amin
Bmean
Cmax
Dmedian
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'min' will give the lowest salary, not the highest.
Using 'mean' will calculate average salary, not maximum.
3fill in blank
hard

Fix the error in the named aggregation to count the number of entries per 'category'.

Pandas
counts = df.groupby('category').agg({'entry_count': ('id', [1])})
Drag options to blanks, or click blank then click option'
Acount
Bmax
Cmean
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' will add values instead of counting.
Using 'mean' or 'max' will not count entries.
4fill in blank
hard

Fill both blanks to group by 'region' and calculate the minimum and maximum of 'sales' using named aggregation.

Pandas
agg_sales = df.groupby('region').agg({'min_sales': ('sales', [1]), 'max_sales': ('sales', [2])})
Drag options to blanks, or click blank then click option'
Amin
Bmax
Cmean
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the functions will give wrong results.
Using 'mean' or 'sum' will not give min or max values.
5fill in blank
hard

Fill all three blanks to group by 'category' and calculate the mean of 'price', sum of 'quantity', and count of 'order_id' using named aggregation.

Pandas
summary = df.groupby('category').agg({'avg_price': ('price', [1]), 'total_quantity': ('quantity', [2]), 'order_count': ('order_id', [3])})
Drag options to blanks, or click blank then click option'
Asum
Bcount
Cmean
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up sum and count functions.
Using max instead of mean for average price.