0
0
Pandasdata~20 mins

GroupBy with pipe for chaining in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GroupBy Pipe Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
Team
B    30
A    25
C    15
Name: Points, dtype: int64
B
Team
C    15
A    25
B    30
Name: Points, dtype: int64
C
Team
A    25
B    30
C    15
Name: Points, dtype: int64
D{'A': 25, 'B': 30, 'C': 15}
Attempts:
2 left
💡 Hint
Remember that pipe passes the object to the function and you can chain operations.
data_output
intermediate
1: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)
A(3, 3)
B(3, 2)
C(5, 3)
D(5, 2)
Attempts:
2 left
💡 Hint
Check how many unique categories and how many aggregated columns.
🔧 Debug
advanced
1: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)
AAttributeError: 'float' object has no attribute 'reset_index'
BNo error, prints a Series
CKeyError: 'Data'
DTypeError: 'DataFrameGroupBy' object is not subscriptable
Attempts:
2 left
💡 Hint
Check what the first pipe returns and what the second pipe tries to do.
🚀 Application
advanced
2: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)
A
  Store  Sales
0    S1    110
1    S2     70
B
  Store  Sales
0    S2     70
1    S3    120
C
  Store  Sales
0    S1    110
1    S3    120
D
  Store  Sales
0    S3    120
Attempts:
2 left
💡 Hint
Sum sales per store, then filter sums greater than 100.
🧠 Conceptual
expert
1:30remaining
Why use pipe in GroupBy chaining?
What is the main advantage of using pipe in pandas GroupBy chaining?
AIt automatically optimizes the GroupBy operation for faster computation.
BIt converts the GroupBy object into a DataFrame immediately.
CIt replaces the need for aggregation functions like sum or mean.
DIt allows applying custom functions in the middle of method chains without breaking the flow.
Attempts:
2 left
💡 Hint
Think about how pipe helps keep code readable and chainable.