Data Analysis with Python: From Raw Data to Real-World Insights
BOOK 2 · PROJECTS · CHAPTER 16

Chapter 16 worked answers: Guided Real-World Investigations

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-16-E01 · Audit measurement completeness

Keep rows with the measurements required for a scatterplot without unnecessarily requiring sex.

A task-specific inclusion rule retains an otherwise valid measurement observation with missing sex.

df=pd.DataFrame({"mass":[3.,4.,None],"length":[1.,2.,3.],"sex":[None,"f","m"]})
usable=df.dropna(subset=["mass","length"])
assert len(usable)==2 and usable.sex.isna().sum()==1
print(usable)
   mass  length   sex
0   3.0     1.0  None
1   4.0     2.0     f

B2-16-E02 · Classify uncertain catalogue dates

Classify before 1900, at/after 1900 and crossing the boundary, without inventing exact dates.

An interval crossing 1900 cannot establish which side contains the date of completion.

early=np.array([1890,1901,1899]);late=np.array([1895,1905,1902])
labels=np.select([late<1900,early>=1900],["before","after"],default="crossing")
assert labels.tolist()==["before","after","crossing"]
print(labels)
['before' 'after' 'crossing']

B2-16-E03 · Retain document identities

Explain why equal titles do not establish duplicate objects; check that IDs are unique and flag title repetition separately.

Different source objects may legitimately share a title. Entity identity and text equality are different contracts.

df=pd.DataFrame({"id":[1,2],"title":["Study","Study"]})
assert df.id.is_unique
repeated=df.title.duplicated(keep=False)
assert repeated.all()
print(repeated.tolist())
[True, True]

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.