Challenge - 5 Problems
GroupBy Pipe Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of GroupBy with pipe chaining
What is the output of this code snippet using pandas GroupBy and pipe chaining?
Pandas
import pandas as pd df = pd.DataFrame({ 'Team': ['A', 'A', 'B', 'B', 'C', 'C'], 'Points': [10, 15, 10, 20, 5, 10], 'Assists': [5, 7, 8, 6, 2, 3] }) result = (df.groupby('Team') .pipe(lambda d: d['Points'].sum()) .pipe(lambda s: s.sort_values(ascending=False))) print(result)
Attempts:
2 left
💡 Hint
Remember that pipe passes the object to the function and you can chain operations.
✗ Incorrect
The code groups by 'Team', sums 'Points' per group, then sorts descending. So B=30, A=25, C=15.
❓ data_output
intermediate1:30remaining
Resulting DataFrame shape after GroupBy with pipe
After running this code, what is the shape of the resulting DataFrame?
Pandas
import pandas as pd df = pd.DataFrame({ 'Category': ['X', 'X', 'Y', 'Y', 'Z'], 'Value': [1, 2, 3, 4, 5], 'Score': [10, 20, 30, 40, 50] }) result = (df.groupby('Category') .pipe(lambda d: d.agg({'Value': 'sum', 'Score': 'mean'})) .reset_index()) print(result.shape)
Attempts:
2 left
💡 Hint
Check how many unique categories and how many aggregated columns.
✗ Incorrect
There are 3 unique categories and 2 aggregated columns plus the reset index column, total 3 columns.
🔧 Debug
advanced1:30remaining
Identify the error in GroupBy pipe chaining
What error will this code raise when executed?
Pandas
import pandas as pd df = pd.DataFrame({ 'Group': ['G1', 'G1', 'G2'], 'Data': [10, 20, 30] }) result = (df.groupby('Group') .pipe(lambda d: d['Data'].mean()) .pipe(lambda s: s.reset_index(drop=True))) print(result)
Attempts:
2 left
💡 Hint
Check what the first pipe returns and what the second pipe tries to do.
✗ Incorrect
No error occurs. The first pipe returns a Series of per-group means (G1: 15.0, G2: 30.0). The second pipe calls reset_index(drop=True) on the Series, which returns a new Series with RangeIndex.
🚀 Application
advanced2:00remaining
Using pipe to filter groups after aggregation
Which option produces a DataFrame with groups having sum of 'Sales' greater than 100 using pipe chaining?
Pandas
import pandas as pd df = pd.DataFrame({ 'Store': ['S1', 'S1', 'S2', 'S2', 'S3'], 'Sales': [50, 60, 40, 30, 120] }) result = (df.groupby('Store') .pipe(lambda d: d['Sales'].sum()) .pipe(lambda s: s[s > 100]) .reset_index()) print(result)
Attempts:
2 left
💡 Hint
Sum sales per store, then filter sums greater than 100.
✗ Incorrect
Stores S1 and S3 have sales sums 110 and 120 respectively, both > 100.
🧠 Conceptual
expert1:30remaining
Why use pipe in GroupBy chaining?
What is the main advantage of using pipe in pandas GroupBy chaining?
Attempts:
2 left
💡 Hint
Think about how pipe helps keep code readable and chainable.
✗ Incorrect
Pipe lets you insert any function into a chain, passing the current object as input, keeping code clean and readable.