5.5.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.
### Question 1: Revenue Trend
# A store manager wants to see how revenue moved over six months.
# 1. Plot the revenue column against month as a line with pandas:
# df.plot(x="month", y="revenue", ...).
# 2. Add circle markers (marker="o") and turn off the legend (legend=False).
# 3. Set the title "Monthly Revenue".
import pandas as pd
df = pd.DataFrame({
"month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"revenue": [120, 135, 128, 150, 162, 158],
})
### Your code starts here.
### Your code ends here.
### Question 2: Regional Comparison
# Compare sales across the four regions.
# 1. Draw a vertical bar chart with df.plot.bar(x="region", y="sales").
# 2. Keep the region labels horizontal (rot=0) and turn off the legend.
# 3. Set the title "Sales by Region".
import pandas as pd
df = pd.DataFrame({
"region": ["East", "West", "South", "North"],
"sales": [240, 210, 180, 150],
})
### Your code starts here.
### Your code ends here.
### Question 3: Score Distribution
# Show how customer survey scores (1 to 10) are distributed.
# 1. Draw a histogram of the score column with .plot.hist().
# 2. Use 4 bins (bins=4).
# 3. Set the title "Survey Scores".
import pandas as pd
df = pd.DataFrame({"score": [2, 4, 6, 8, 7, 3, 9, 5, 6, 7, 8, 6]})
### Your code starts here.
### Your code ends here.
### Question 4: Ad Spend and Revenue
# Check whether higher ad spending goes with higher revenue.
# 1. Draw a scatter plot with df.plot.scatter(x="ad_spend", y="revenue").
# 2. Set the title "Ad Spend vs Revenue".
# 3. Set the x-label "Ad Spend ($K)" and the y-label "Revenue ($K)"
# (the xlabel= and ylabel= arguments).
import pandas as pd
df = pd.DataFrame({
"ad_spend": [5, 10, 15, 20, 25, 30],
"revenue": [60, 85, 110, 120, 150, 165],
})
### Your code starts here.
### Your code ends here.
### Question 5: Styled Comparison
# Compare online and in-store sales on one chart sized for a report.
# 1. Plot both columns against month with df.plot(x="month", y=["online", "store"]).
# 2. Make the figure 8 by 4 inches (figsize=(8, 4)).
# 3. Draw online solid and store dashed with style=["-", "--"].
# 4. Set the title "Online vs Store Sales" and the y-label "Sales ($K)".
import pandas as pd
df = pd.DataFrame({
"month": ["Jan", "Feb", "Mar", "Apr"],
"online": [40, 45, 52, 60],
"store": [70, 68, 66, 65],
})
### Your code starts here.
### Your code ends here.
Question 1
Question 2
Question 3
Question 4
Question 5