0
0
Pandasdata~20 mins

Pivot with aggregation functions in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pivot Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
Type       X   Y
Category         
A         10  20
B         30  40
C          0  50
B
Type       X   Y
Category         
A         10  20
B         30  40
C         50   0
C
Type       X   Y
Category         
A         30  60
B         30  40
C         50   0
D
Type       X   Y
Category         
A         10  20
B         40  30
C         50   0
Attempts:
2 left
💡 Hint
Remember that pivot_table with aggfunc='sum' adds values grouped by index and columns.
data_output
intermediate
1: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')
A6
B9
C4
D3
Attempts:
2 left
💡 Hint
Count unique index values times unique columns values.
🔧 Debug
advanced
2: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)
ANo error, prints pivot table with multi-level columns
BTypeError: unhashable type: 'list'
CValueError: aggfunc must be a function or list of functions
DKeyError: 'Value'
Attempts:
2 left
💡 Hint
Check if pandas supports list of functions in aggfunc parameter.
🚀 Application
advanced
1: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'
Adf.pivot_table(index='Region', columns='Product', values='Sales', aggfunc='min')
Bdf.pivot_table(index='Product', columns='Region', values='Sales', aggfunc='max')
Cdf.pivot_table(index='Region', columns='Product', values='Sales', aggfunc='sum')
Ddf.pivot_table(index='Region', columns='Product', values='Sales', aggfunc='max')
Attempts:
2 left
💡 Hint
Index should be 'Region' to group rows by region.
🧠 Conceptual
expert
1: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?
AIt causes a runtime error if missing data exists.
BIt fills missing values with the mean of the column.
CIt replaces missing values in the pivot table with 0 instead of NaN.
DIt removes rows and columns with missing data from the pivot table.
Attempts:
2 left
💡 Hint
Think about how missing data is shown in pivot tables by default.