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,Noneand<NA>, and test for any of them withisna - Count gaps per column with
isna().sum() - Drop rows with
dropna, choosing which columns matter withsubset - Fill gaps with
fillna, and say when that’s honest
Three ways to say missing
Run the cells.
pandas has three markers for a gap, depending on the column's type:
NaNin float columns, from NumPy (lesson 2.3). ANonein a list of numbers becomesNaN.NaNinstrcolumns too: in pandas 3, a missing piece of text is shown asNaN.<NA>in the nullable types likeInt64(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:
Now the same in code. Run the cells.
- 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.Truecounts 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.
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
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.
- pandas: Working with missing data: NaN, None and NA; isna, dropna and fillna
- pandas: pandas.DataFrame.dropna: how, thresh and subset
- pandas: pandas.DataFrame.fillna: a value per column, from a dictionary
- Rubin, Inference and missing data, Biometrika 63(3), 1976: why it matters whether data is missing at random