0
0
Pandasdata~10 mins

Pivot with aggregation functions 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 create a pivot table that sums the 'Sales' for each 'Region'.

Pandas
pivot_table = df.pivot_table(index='Region', values='Sales', aggfunc=[1])
Drag options to blanks, or click blank then click option'
Asum
Bmean
Ccount
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mean' instead of 'sum' will calculate averages, not totals.
Using 'count' will count entries, not sum values.
2fill in blank
medium

Complete the code to create a pivot table that calculates the average 'Sales' for each 'Product'.

Pandas
pivot_table = df.pivot_table(index='Product', values='Sales', aggfunc=[1])
Drag options to blanks, or click blank then click option'
Amin
Bmean
Csum
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' will add all sales instead of averaging.
Using 'min' or 'max' will find smallest or largest values, not average.
3fill in blank
hard

Fix the error in the code to count the number of sales entries per 'Region'.

Pandas
pivot_table = df.pivot_table(index='Region', values='Sales', aggfunc=[1])
Drag options to blanks, or click blank then click option'
Asum
Bmedian
Cmean
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' will add sales values instead of counting entries.
Using 'mean' or 'median' will calculate averages, not counts.
4fill in blank
hard

Fill both blanks to create a pivot table that shows the maximum 'Sales' for each 'Region' and 'Product'.

Pandas
pivot_table = df.pivot_table(index=[1], columns=[2], values='Sales', aggfunc='max')
Drag options to blanks, or click blank then click option'
A'Region'
B'Sales'
C'Product'
D'Date'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'Region' and 'Product' will change the pivot table layout.
Using 'Sales' or 'Date' as index or columns will cause errors or wrong grouping.
5fill in blank
hard

Fill all three blanks to create a pivot table that calculates the mean 'Sales' for each 'Region' and 'Product', and fills missing values with 0.

Pandas
pivot_table = df.pivot_table(index=[1], columns=[2], values=[3], aggfunc='mean').fillna(0)
Drag options to blanks, or click blank then click option'
A'Region'
B'Product'
C'Sales'
D'Date'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong columns for index or columns changes the table layout.
Not filling missing values can leave NaNs in the result.