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.


