Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 14.7 — Loading and Cleaning Data
Python · Example 2 of 3

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.

01

Load a CSV text with columns item,price where one price is missing, and fill the missing price with the median.

02

Clean the text column [" PEN ", "Book", "pen"] so all three entries match, and count the unique values.

03

Convert the strings ["2024-03-01", "oops"] to dates with coerce and count the missing results.

04

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

01

How do you find missing values in a DataFrame, and what are your options for handling them?

02

Why can an integer column show as floats after read_csv?

03

What does errors="coerce" do in to_datetime? When is it dangerous?

04

Why does removing duplicates matter before training a model?

05

A CSV has a country column where Namibia (NA) shows up as missing after read_csv. Why, and how do you load it correctly?

06

How do missing values behave in pandas sums and means compared with NumPy, and what is pd.NA?

Clean it: duplicates, text, gaps, dates
Sign in to run this example in your browser.

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