Chapter 2 quiz
Ten questions to bring the chapter together.
Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.
Question 1
The manager needs the Price column as a two-dimensional table. Print its type name and shape.
Write your answer, then run it.
Question 2
Print the Price value in the third row by numerical position, then print rows B through D using their labels.
Write your answer, then run it.
Question 3
The purchasing manager needs Central records with at least three units. Print only Store and Units for those records.
Write your answer, then run it.
Question 4
Report the total observed Sales and the number of records used. Preserve the recorded zero and exclude unknown Sales.
Write your answer, then run it.
Question 5
Create Revenue and ProductCost, then report total gross profit for these records.
Write your answer, then run it.
Question 6
These orders include one large transaction. Print the mean and median, then state which better describes the middle observation.
Write your answer, then run it.
Question 7
Print category totals and category means. Identify the category contributing the larger total and the category with the larger average.
Write your answer, then run it.
Question 8
Arrange these records chronologically and print percentage changes from the previous observation.
Write your answer, then run it.
Question 9
A manager asks for the recent average and the total accumulated so far. Print the final three-observation mean and cumulative total.
Write your answer, then run it.
Question 10
An investment increases 10% and then decreases 10%. Starting at HKD 100, calculate and print the ending value and total percentage return. Explain why adding the two rates fails.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
Question 1
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
result = df[["Price"]]
print(type(result).__name__)
print(result.shape)Expected output
DataFrame
(4, 1)A list of column names preserves a DataFrame.
Question 2
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
print(df.iloc[2, 2])
print(df.loc["B":"D"])Expected output
35
Store Units Price
B Kowloon 1 80
C Central 4 35
D Kowloon 3 60Positional counting starts at zero; the label slice includes both endpoints.
Question 3
import pandas as pd
df = pd.DataFrame({"Store": ["Central", "Kowloon", "Central", "Kowloon"],
"Units": [2, 1, 4, 3], "Price": [120, 80, 35, 60]},
index=["A", "B", "C", "D"])
mask = (df["Store"] == "Central") & (df["Units"] >= 3)
print(df.loc[mask, ["Store", "Units"]])Expected output
Store Units
C Central 4The row condition and requested output columns are separate selections.
Question 4
import pandas as pd
df = pd.DataFrame({"Order": ["A", "B", "C", "D"],
"Sales": [100, None, 0, 300],
"Rating": [4.0, 5.0, None, 3.0]})
observed = df.dropna(subset=["Sales"])
print(observed["Sales"].sum())
print(len(observed))Expected output
400.0
3Unknown ratings do not prevent an observed-sales calculation.
Question 5
import pandas as pd
df = pd.DataFrame({"Units": [2, 3, 1], "Price": [120, 40, 90], "UnitCost": [70, 22, 50]})
df["Revenue"] = df["Units"] * df["Price"]
df["ProductCost"] = df["Units"] * df["UnitCost"]
print((df["Revenue"] - df["ProductCost"]).sum())Expected output
194Gross profit subtracts product cost; other expenses remain outside this measure.
Question 6
import pandas as pd
sales = pd.Series([80, 100, 120, 140, 1000])
print(sales.mean())
print(sales.median())
print("The median describes the middle observation.")Expected output
288.0
120.0
The median describes the middle observation.The large transaction raises the mean.
Question 7
import pandas as pd
df = pd.DataFrame(
{
"Category": ["Gifts", "Gifts", "Gifts", "Accessories"],
"Revenue": [100, 100, 100, 200],
}
)
gifts = df.loc[df["Category"] == "Gifts", "Revenue"]
accessories = df.loc[df["Category"] == "Accessories", "Revenue"]
totals = pd.Series({"Gifts": gifts.sum(), "Accessories": accessories.sum()})
means = pd.Series({"Gifts": gifts.mean(), "Accessories": accessories.mean()})
print(totals)
print(means)
print(totals.idxmax(), means.idxmax())Expected output
Gifts 300
Accessories 200
dtype: int64
Gifts 100.0
Accessories 200.0
dtype: float64
Gifts AccessoriesGroup size explains why the rankings differ.
Question 8
import pandas as pd
df = pd.DataFrame(
{"Date": ["2026-11-03", "2026-11-01", "2026-11-02"], "Sales": [90.0, 100.0, 120.0]}
)
df["Date"] = pd.to_datetime(df["Date"])
dated = df.set_index("Date").sort_index()
print(list((dated["Sales"].pct_change(fill_method=None) * 100).round(2)))Expected output
[nan, 20.0, -25.0]The dates must be ordered before a previous-observation calculation.
Question 9
import pandas as pd
sales = pd.Series([100, 200, 300, 400, 500])
print(sales.rolling(3).mean().iloc[-1])
print(sales.cumsum().iloc[-1])Expected output
400.0
1500Recent average is 400 per observation; the accumulated total is 1500.
Question 10
import pandas as pd
returns = pd.Series([0.10, -0.10])
factors = (1 + returns).cumprod()
print(round(100 * factors.iloc[-1], 2))
print(round((factors.iloc[-1] - 1) * 100, 2))
print("The second rate applies to the changed value.")Expected output
99.0
-1.0
The second rate applies to the changed value.The ending value is 99 and combined return is -1%.