Challenge - 5 Problems
Pivot Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pivot with sum aggregation
What is the output of this code snippet using pandas pivot_table with sum aggregation?
Pandas
import pandas as pd data = {'Category': ['A', 'A', 'B', 'B', 'C'], 'Type': ['X', 'Y', 'X', 'Y', 'X'], 'Value': [10, 20, 30, 40, 50]} df = pd.DataFrame(data) pivot = df.pivot_table(index='Category', columns='Type', values='Value', aggfunc='sum', fill_value=0) print(pivot)
Attempts:
2 left
💡 Hint
Remember that pivot_table with aggfunc='sum' adds values grouped by index and columns.
✗ Incorrect
The pivot_table groups by 'Category' as rows and 'Type' as columns, summing 'Value'. Missing combinations fill with 0.
❓ data_output
intermediate1:30remaining
Number of items in pivot result with mean aggregation
How many cells (data points) are in the resulting DataFrame after this pivot_table operation?
Pandas
import pandas as pd data = {'Group': ['G1', 'G1', 'G2', 'G2', 'G3', 'G3'], 'Category': ['A', 'B', 'A', 'B', 'A', 'B'], 'Score': [5, 10, 15, 20, 25, 30]} df = pd.DataFrame(data) pivot = df.pivot_table(index='Group', columns='Category', values='Score', aggfunc='mean')
Attempts:
2 left
💡 Hint
Count unique index values times unique columns values.
✗ Incorrect
There are 3 unique Groups and 2 unique Categories, so 3*2=6 cells in the pivot table.
🔧 Debug
advanced2:00remaining
Identify the error in pivot_table with multiple aggfuncs
What error does this code raise when trying to pivot with multiple aggregation functions?
Pandas
import pandas as pd data = {'Category': ['A', 'A', 'B', 'B'], 'Type': ['X', 'Y', 'X', 'Y'], 'Value': [1, 2, 3, 4]} df = pd.DataFrame(data) pivot = df.pivot_table(index='Category', columns='Type', values='Value', aggfunc=['sum', 'mean']) print(pivot)
Attempts:
2 left
💡 Hint
Check if pandas supports list of functions in aggfunc parameter.
✗ Incorrect
pandas pivot_table supports multiple aggregation functions as a list, producing multi-level columns without error.
🚀 Application
advanced1:30remaining
Choose correct pivot_table to find max sales per region and product
Given a DataFrame with columns 'Region', 'Product', and 'Sales', which pivot_table code correctly finds the maximum sales for each region and product?
Pandas
import pandas as pd # Assume df is given with columns 'Region', 'Product', 'Sales'
Attempts:
2 left
💡 Hint
Index should be 'Region' to group rows by region.
✗ Incorrect
To find max sales per region and product, index must be 'Region', columns 'Product', aggfunc 'max'.
🧠 Conceptual
expert1:00remaining
Understanding fill_value effect in pivot_table
What is the effect of setting fill_value=0 in a pandas pivot_table when some combinations of index and columns have no data?
Attempts:
2 left
💡 Hint
Think about how missing data is shown in pivot tables by default.
✗ Incorrect
By default, missing combinations show as NaN. fill_value=0 replaces these NaNs with zero.