Missing Values

Find them, then decide between dropping and filling on purpose.

Find them first

You cannot decide what to do about a gap you have not counted. How many, and in which column, changes the answer between dropping and filling.

rows = [{"score": "92"}, {"score": ""}, {"score": "88"}]
missing = [r for r in rows if r["score"] == ""]
print(len(missing), "of", len(rows))

Drop or fill, and say which

Dropping loses a row. Filling with an average keeps it and pulls the average toward itself. Neither is wrong; doing it without noticing is.

scores = [92, 88, 0]
kept = [s for s in scores if s != 0]
print(kept)
print(sum(kept) / len(kept))

Exercise

Try It Yourself

Write drop_missing(rows) that returns a new list containing only the rows whose score is not an empty string. Leave the original list alone.

Press Run to see output

Check Your Understanding