Skip to content
dreamcode
dreamcode
Map
Data cleaning
Lesson 75 of 77
+15 XP on finish
PYTHON APPLIEDChapter 13 · Python for Data Science

Loading and cleaning messy data

Real-world data is messy. Columns may have missing values (NaN), incorrect types, or inconsistent formatting. Pandas provides tools to detect, fill, drop, and convert bad data before analysis.

Worked example

How it reads

  • pd.to_numeric(..., errors='coerce') converts invalid strings to NaN
  • .fillna(median) replaces missing values with the column's median
  • .dropna(subset=['city']) removes rows where city is missing
Cloud tip: Always inspect your data with df.info() and df.isna().sum() before analysis to understand the scope of missing or mistyped values.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add a temp column from the readings ["18", "x", "21"] with pd.to_numeric(..., errors="coerce"), fill the missing value with 0, then print weather["temp"].tolist().

Press Run to check your work.