Data Cleaning with Pandas
Rename columns, fix data types, remove duplicates, and standardise messy real-world data
I want to be direct with you about something that no job description or university course prepares you for: data cleaning is the job. Not dashboards. Not machine learning. Not beautiful charts. Cleaning.
In twenty years of working with data teams, I have never once received a dataset that was ready to analyse. Not from a multinational corporation. Not from a well-funded startup. Not from a government ministry. Every single dataset had something wrong — column names with trailing spaces, numbers stored as text, dates in three different formats in the same column, duplicate rows from system bugs, encoding errors from someone exporting a Hindi-language field through the wrong codepage.
The analysts who are fast and trusted are the ones who have a systematic cleaning process they run on every dataset, every time, without skipping steps. The ones who struggle are the ones who eyeball the data, miss something, and spend two days debugging a pivot table that was silently summing null values as zero.
This tutorial gives you that systematic process. Memorise it. Run it on every new dataset before you do anything else. Column names first. Data types second. Duplicates third. Whitespace fourth. Nulls fifth. In that order, every time. It will save you more hours than any other skill in this series.
Example
Key Points
- ✓Standardise column names first — lowercase with underscores prevents bugs
- ✓errors="coerce" in pd.to_numeric and pd.to_datetime silently handles bad values as NaN
- ✓drop_duplicates(subset=["id_col"]) is safer than dropping all-column duplicates
- ✓select_dtypes(include="object") selects all text columns at once
- ✓Always check df.shape before and after cleaning — confirm row counts are as expected
Practice Question
A "Sales" column was loaded as object (text) due to some rows having "N/A" as text. Which is the correct conversion?