Python practice: GroupBy Aggregations & Named Multi-Metric Reports
Chapter 25 · 8 questions · included in full access
Question 1 · easy
Split-apply-combine concept
Solve it in your browserIn pandas, what happens during the three stages of the split-apply-combine pattern when calling
df.groupby("Region")["Revenue"].sum()?Question 2 · easy
Calculate mean revenue by region
Solve it in your browserLoad
"sales.csv". Calculate the averageRevenuefor eachRegionusing.groupby("Region")["Revenue"].mean(). Print the average revenue for"North"rounded to 2 decimal places usinground(means["North"], 2):Question 3 · medium
Two-column grouping: Region and Category
Solve it in your browserLoad
"sales.csv". Group by["Region", "Category"]and sum"Revenue". Print the total revenue for the("North", "Sheets")combination:Question 4 · easy
Tally group frequencies with size()
Solve it in your browserLoad
"sales.csv". Group by"Region"and call.size()to count rows per region. Print the count for"South":Question 5 · medium
Multi-metric aggregation with .agg()
Solve it in your browserLoad
"sales.csv". For eachCategory, compute the minimum and maximumRevenueusing.groupby("Category")["Revenue"].agg(["min", "max"]). Print the maximum revenue for"Sheets":Question 6 · medium
Named aggregation with custom output columns
Solve it in your browserLoad
"sales.csv". Use named aggregation on.groupby("Region")to calculatetotal_rev=("Revenue", "sum")andorder_count=("OrderID", "count"). Print thetotal_revfor"West":Question 7 · easy
Reset index to produce flat export table
Solve it in your browserLoad
"sales.csv". SumRevenuebyRegion, call.reset_index(), and print the column names of the resulting DataFrame as a list (list(flat.columns)):Question 8 · hard
Executive sales performance report
Solve it in your browserLoad
"sales.csv". Group by"Rep"and use.agg()to computetotal_rev=("Revenue", "sum")andorders=("OrderID", "count"). Reset the index, sort by"total_rev"descending, and print the top rep name (report.iloc[0]["Rep"]):
These questions are part of full access.
The first chapters of every track are free. Full access is a one-time payment.
See pricing