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

Chapter 8 exercises: Cleaning and Validating Data

On this page

Attempt these tasks before opening the worked answers. Use the chapter experiment notebook, or import NumPy as np and Pandas as pd in your own notebook. Retain your first attempt and explain each correction.

B2-08-E01 · Separate parsing and validation

Parse the numeric values and flag both unresolved and negative counts.

s=pd.Series(["12","unknown","-2"])
One hint after an attempt

Use two distinct checks.

B2-08-E02 · Investigate key conflicts

Identify all conflicting ID rows without silently choosing one.

df=pd.DataFrame({"id":["a","a","b"],"value":[2,3,4]})
One hint after an attempt

keep=False marks every member of a repeated-key group.

B2-08-E03 · Preserve missingness semantics

Normalize institution spacing and retain the distinction between missing visits and zero visits.

df=pd.DataFrame({"institution":[" N ","S",None],"visits":[2,None,0]})
One hint after an attempt

Clean the label column without filling the count column.

Reflect and get feedback

Explain one result without looking at the code. Identify one boundary case that would change your method. An optional tutor prompt is: “Here is my attempt at B2-08-E03. Give one hint about the violated assumption; do not give the solution yet.” Then use the worked answers.

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