Thuta Learning
IntermediateData & Databasesintermediate

Handling Missing Data

Relax. We'll talk through this in plain words — no textbook voice.

Handling missing data

Real-world data often has spots where values are missing. In Pandas, you'll typically see missing values shown as NaN. If you run calculations without checking for missing data first, you can end up with incorrect results, charts that don't render, or a model that trains incorrectly.

  • .isna() — checks whether values are missing
  • .dropna() — removes rows/columns that contain missing values
  • .fillna() — fills missing values with a specified value
python
import pandas as pd
import numpy as np

df = pd.DataFrame({
    "Product": ["Tea", "Coffee", "Cake"],
    "Price": [1200, np.nan, 2500],
    "Qty": [3, 2, np.nan]
})

print("Missing values per column:")
print(df.isna().sum())

filled = df.fillna({"Price": 0, "Qty": 0})
print("
After filling missing values:")
print(filled)

df.isna().sum() shows how many missing values there are in each column. fillna() lets you use a dictionary to set a different fill value for each column.

You should see
Missing values per column: Product 0 Price 1 Qty 1 dtype: int64 After filling missing values: Product Price Qty 0 Tea 1200.0 3.0 1 Coffee 0.0 2.0 2 Cake 2500.0 0.0

Easy traps

  • Always reaching for dropna() isn't a good habit. Removing rows can lose important data. Think about why the value is missing first, then decide whether to drop it or fill it in.