Wrangling & cleaning — quiz
8 questions covering this module. Missing values, duplicates, text and types.
mean() on 100 values with 40 missing divides by:
- 100
- 60
- 40
- It errors
Answer: 60 — Missing values are skipped entirely.
df.dropna() with no arguments removes:
- Empty columns
- Any row containing at least one NaN
- Only fully empty rows
- Duplicates
Answer: Any row containing at least one NaN — On a wide table this can remove most of the data.
To keep the most recent record per email:
- drop_duplicates() alone
- Sort by date, then drop_duplicates(keep="last")
- dropna()
- groupby(email)
Answer: Sort by date, then drop_duplicates(keep="last") — Without sorting, which row survives is arbitrary.
duplicated(keep=False) marks:
- The first copy only
- Every copy including the first
- Nothing
- Only nulls
Answer: Every copy including the first — Useful for inspecting all duplicates together.
Why does str.contains() fail on some columns?
- Wrong dtype
- The column contains NaN — pass na=False
- Regex not enabled
- Column too long
Answer: The column contains NaN — pass na=False — Missing values raise unless you tell it how to treat them.
str.split(" ", n=1, expand=True) returns:
- A list
- A DataFrame of the split parts
- A string
- A Series of lists
Answer: A DataFrame of the split parts — expand=True gives columns you can assign directly.
errors="coerce" does what to unparseable values?
- Raises an error
- Converts them to NaN
- Leaves them as text
- Drops the row
Answer: Converts them to NaN — Which lets the rest of the column convert.
Why pass format= to to_datetime?
- It is faster only
- To avoid ambiguous day/month misparsing
- It is required
- To handle nulls
Answer: To avoid ambiguous day/month misparsing — 03/04/2024 is ambiguous, and pandas may guess inconsistently.