In tutorials, datasets are always clean, perfectly formatted tables. In the real world, almost every dataset comes with missing values, messy text, and strange numbers.
Here are three simple patterns I use all the time in Python to clean data.
1. Handling Missing Data
Dropping rows with df.dropna() is tempting, but you often lose too much data. Filling in sensible defaults is usually safer:
# Fill missing numbers with the median
df['price'] = df['price'].fillna(df['price'].median())
# Fill missing text with a placeholder
df['status'] = df['status'].fillna('Unknown')2. Standardizing Messy Text
Extra spaces and inconsistent letter casing will break your calculations (for example, treating " Kathmandu" and "kathmandu" as different places).
# Lowercase and remove extra whitespace in one go
df['city'] = df['city'].str.strip().str.lower()3. Catching Outliers
A typo like an age of 250 or revenue of -$50,000 will distort all your averages. Look at summary statistics first:
# Fast five-number summary of your columns
df.describe()If a number looks completely impossible, filter it out or investigate where it came from before running your analysis.
Takeaway
Data cleaning is not glamorous, but it is the foundation of every reliable analysis. If your source data is messy, even the best models and charts will give the wrong answers.