7.4.2. Lab#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
Keep the specified printed output and plots unchanged.
You are preparing a short retail operations review. The questions use separate small datasets from sales, marketing, finance, operations, and regional management. Each released solution notes what its chart shows and what the data cannot establish.
### Question 1: Order Value Distribution
# The online store manager wants to see how order values are spread out.
# 1. Create a figure and axes with plt.subplots().
# 2. Use sns.histplot() to draw a histogram of the order_value column
# of orders with bins=6 on that axes (ax=ax).
# 3. Set the title "Order Values".
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
orders = pd.DataFrame({
"order_value": [45, 52, 38, 61, 75, 49, 58, 66, 40, 55, 70, 48],
})
### Your code starts here.
### Your code ends here.
### Question 2: Ad Spend and Revenue by Region
# Marketing wants to know whether ad spend pays off the same way in both regions.
# 1. Create a figure and axes with plt.subplots().
# 2. Use sns.scatterplot() to plot ad_spend (x) vs revenue (y) from campaigns,
# with the points colored by region (hue='region').
# 3. Set the title "Ad Spend vs Revenue".
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
campaigns = pd.DataFrame({
"ad_spend": [5, 10, 15, 20, 25, 30, 35, 40],
"revenue": [60, 70, 110, 105, 150, 140, 190, 170],
"region": ["North", "South", "North", "South", "North", "South", "North", "South"],
})
### Your code starts here.
### Your code ends here.
### Question 3: From Wide to Tidy
# Finance keeps quarterly revenue in wide format: one column per region.
# Seaborn needs tidy (long) data to color bars by region.
# 1. Reshape sales with sales.melt(id_vars='quarter', var_name='region',
# value_name='revenue') and store the result in tidy.
# 2. Print the number of rows in tidy.
# 3. Create a figure and axes, then use sns.barplot() to draw revenue (y)
# by quarter (x) with the bars colored by region (hue='region').
# Print exactly:
# Tidy rows: 8
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
sales = pd.DataFrame({
"quarter": ["Q1", "Q2", "Q3", "Q4"],
"North": [150, 165, 158, 180],
"South": [120, 118, 135, 142],
})
### Your code starts here.
### Your code ends here.
### Question 4: Delivery Days by Shipping Method
# Operations wants to compare how long each shipping method takes.
# 1. Create a figure and axes with plt.subplots().
# 2. Use sns.boxplot() to draw days (y) by method (x) from deliveries.
# 3. Set the title "Delivery Days by Method".
# 4. Find the method with the lowest median delivery days using
# groupby() and median(), then print it.
# Print exactly:
# Fastest median: Overnight
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
deliveries = pd.DataFrame({
"method": ["Standard"] * 5 + ["Express"] * 5 + ["Overnight"] * 5,
"days": [5, 6, 5, 7, 6, 2, 4, 3, 5, 2, 1, 1, 2, 1, 1],
})
### Your code starts here.
### Your code ends here.
### Question 5: Discount Response by Region
# A regional director wants one panel per region showing how discount
# relates to units sold.
# 1. Use the figure-level function sns.relplot() to draw a scatter plot
# of discount (x) vs units (y) from promo.
# 2. Put each region in its own column (col='region') and use height=3.
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
promo = pd.DataFrame({
"discount": [5, 10, 15, 20] * 3,
"units": [40, 52, 61, 75, 38, 41, 45, 47, 30, 45, 58, 70],
"region": ["East"] * 4 + ["West"] * 4 + ["North"] * 4,
})
### Your code starts here.
### Your code ends here.
Question 1
Question 2
Question 3
Question 4
Question 5