← Learning Hub
Intermediate

Cleaning Messy Data

Real data is messy: blanks, repeats, extra spaces, mixed capital letters. Cleaning usually takes more time than the analysis itself. Here is a messy staff file:

Analysts often spend most of their time cleaning: missing values, duplicates, extra spaces, inconsistent spellings, numbers stored as text. Clean data gives correct answers; dirty data gives confident wrong ones. Always check, clean, and note what you changed.

Example
name,city,age,salary,joined
Asha Verma, delhi ,29,52000,2024-04-01
Rahul Mehta,Mumbai,,61000,2023-11-15
Sara Khan,PUNE,34,,2022-07-20
Asha Verma, delhi ,29,52000,2024-04-01
Vikram Rao,Chennai,41,78000,2021-01-10
Neha Gupta,mumbai,26,45000,

Find the problems

Example
You can edit this code.

Fix them

Example
You can edit this code.

Text tools: .str

Example
You can edit this code.

Renaming and changing values

Example
You can edit this code.
Decide carefully: filling a blank with the median is a guess. Sometimes dropping the row, or asking where the data came from, is better. Write down every cleaning step you take.
Key points
  • isna().sum() and duplicated() find problems.
  • fillna, dropna, drop_duplicates and str methods fix them.
  • Write down every cleaning decision.

📝 Test yourself

1. Which counts blanks in each column?
2. What does df.drop_duplicates() do?
3. Fill in the method that fills blanks:
df["age"] = df["age"].(30)