Clean it: duplicates, text, gaps, dates
Change the age fill from median to mean and see what changes. Explain why the median is the usual safer choice.
Real data arrives messy: missing values, duplicates, stray spaces, text where numbers should be and dates stored as strings. read_csv loads it, and a handful of pandas methods clean it before analysis or model training.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Load a CSV text with columns item,price where one price is missing, and fill the missing price with the median.
Clean the text column [" PEN ", "Book", "pen"] so all three entries match, and count the unique values.
Convert the strings ["2024-03-01", "oops"] to dates with coerce and count the missing results.
Load the CSV text code,n / NA,1 / IN,2 so that the code NA stays a real string, and print the list of codes.
Explain it without notes
How do you find missing values in a DataFrame, and what are your options for handling them?
Why can an integer column show as floats after read_csv?
What does errors="coerce" do in to_datetime? When is it dangerous?
Why does removing duplicates matter before training a model?
A CSV has a country column where Namibia (NA) shows up as missing after read_csv. Why, and how do you load it correctly?
How do missing values behave in pandas sums and means compared with NumPy, and what is pd.NA?
Expected output
id name age joined score month
0 1 Asha 23 2024-01-15 88.5 1
1 2 Ben 27 2024-02-20 92.0 2
2 3 Chen 27 2024-02-20 0.0 2
4 4 Dia 45 NaT 70.0 <NA>
id int64
name object
age int64
joined datetime64[ns]
score float64
month Int64
dtype: object