pandas basics · lesson 8 of 9
Parts in this lesson
Adf.isna().sum()gaps in each column
Bdf.dropna(subset=["kilos"])drop rows missing what you need
Cdf.fillna({"note": ""})fill gaps, column by column

Missing values, first look

Last updated

Almost every real table has gaps: a store that didn't report, a price nobody typed, a note left blank. In this lesson you'll learn how pandas marks a gap, find and count them, and choose between dropping and filling, knowing what each one costs.

By the end of this lesson you can

  • Recognise NaN, None and <NA>, and test for any of them with isna
  • Count gaps per column with isna().sum()
  • Drop rows with dropna, choosing which columns matter with subset
  • Fill gaps with fillna, and say when that’s honest

Three ways to say missing

Run the cells.

kinds.ipynb

pandas has three markers for a gap, depending on the column's type:

  • NaN in float columns, from NumPy (lesson 2.3). A None in a list of numbers becomes NaN.
  • NaN in str columns too: in pandas 3, a missing piece of text is shown as NaN.
  • <NA> in the nullable types like Int64 (lesson 3.7).

You don't need to remember which is which, because pd.isna says True for all three, as the last cell shows. Never test for a gap with == np.nan; lesson 2.3 showed why that's always False.

Find, count, drop, fill

A week of sales, with gaps. First, a picture of where they are:

A map of the missing values. The week’s sales file as a map: every cell a box, ten rows by five columns. The dark boxes are missing.weekdatestorekilospricenote0123456789
1 of 4 The week’s sales file as a map: every cell a box, ten rows by five columns. The dark boxes are missing.

Now the same in code. Run the cells.

week.ipynb
  • The table shows the gaps as NaN, in pink.
  • isna().sum() counts the gaps per column: 2 in kilos, 1 in price, 5 in note. True counts as 1, as in lesson 2.10.
  • isna().mean() gives the share missing: half the notes.
  • dropna(subset=["kilos"]) drops only the rows missing kilos. 8 rows are left.
  • fillna({...}) fills each column its own way. An empty note is fine as "". The missing price is Lipa's, and Lipa charged ₱46.00 every other day, so filling 46.0 is a reasonable guess; write in the notebook that you did.

Fill only when you can say why the value is right. A missing kilos figure can't be filled honestly: nobody knows what Tanauan sold that day. Filling it with 0 would say it sold nothing; filling it with the average would invent a day that didn't happen. Stage 5 looks at better ways, and at when they're safe.

Wrong way: dropna on everything

dropna() with no arguments looks like a quick way to "clean" a table. Run the cells.

clean.ipynb

Four rows left out of ten: 60% of the data gone. dropna() drops every row with a gap in any column, and the note column, which nobody needed for working out sales, was blank in half the rows. Lipa's day with no price went too, though its kilos were fine. Every total and average from clean would be wrong.

It's worse than losing rows. Look at which ones went: rows without a note. If notes were mostly written on unusual days (rain, holidays), the rows you kept are the unusual ones, and the "clean" average describes odd days, not normal ones. When what's missing isn't random, dropping it changes the answer, not just the amount of data.

The last cell asks the right question: which columns does this analysis need? Kilos and price. dropna(subset=["kilos", "price"]) keeps 7 rows. Always check the shape before and after a dropna, and write down how many rows went and why.

Check what you learned

How do you count the missing values in each column of df?
A table has 1,000 rows. A comments column is empty in 700 of them. What does df.dropna() leave?
One day’s kilos is missing. When is filling it with 0 right?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis