Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · APPLIED ANALYSIS · CHAPTER 13

Chapter 13 worked answers: Reproducible Analysis and Reporting

On this page

Compare your reasoning after an attempt. Different valid implementations are welcome when they preserve the observation set and the stated meaning.

B2-13-E01 · Fail on an invalid contract

Write a validation function that raises ValueError when id is not unique.

A clear failure prevents an invalid observation set from entering a downstream report.

df=pd.DataFrame({"id":[1,1],"value":[3,4]})
def validate(frame):
    if not frame.id.is_unique:raise ValueError("Repeated ID")
try:validate(df)
except ValueError:print("rejected")
else:raise AssertionError("Expected failure")
rejected

B2-13-E02 · Combine partial means correctly

Compute the overall mean from sums and counts, and demonstrate why the equal mean of chunk means differs.

Chunk sizes differ, so equal chunk weighting changes the target quantity.

chunks=[np.array([2.,4.]),np.array([10.])]
overall=sum(c.sum() for c in chunks)/sum(c.size for c in chunks)
wrong=np.mean([c.mean() for c in chunks])
assert np.isclose(overall,16/3) and wrong==6.5
print(overall,wrong)
5.333333333333333 6.5

B2-13-E03 · Preserve transformation inputs

Write a function returning a doubled value column without mutating source.

Returning a new table makes inputs and outputs visible and helps reproducibility.

source=pd.DataFrame({"id":[1,2],"value":[4,8]})
def transform(frame):return frame.assign(doubled=frame.value*2)
result=transform(source)
assert "doubled" not in source and result.doubled.tolist()==[8,16]
print(result)
   id  value  doubled
0   1      4        8
1   2      8       16

Return to practice

Change one supplied input, predict the effect and rerun your own implementation. Return to the exercise prompts to identify a task you can now explain independently.

Teaching edition · verified experiments · PDF and EPUB editions not generated.