Python data cleaning: zero is not a missing value
By Future Data Academy · 3 min read · Updated
Work through a small Python example that keeps zero, handles missing values and tests empty input without hiding data-quality problems.
A cleaning rule can quietly change the question your analysis answers. One common mistake is to remove every value that evaluates as false. In Python, zero is false-like, but zero sales and an unrecorded sale amount are different observations.
This exercise uses a small synthetic list. It assumes that present values are numeric; it is not a complete CSV importer or a universal missing-data policy.
Work out the expected answer first
Take the values [0, 5, None, 15]. There are four input rows, three observed numbers and one missing value. The observed total is 20 and the mean of the observed values is 20/3, approximately 6.67. Dividing by four would treat the missing value as if it contributed an observed zero.
The list comprehension [x for x in values if x] would remove both None and 0. Its remaining mean would be 10, answering a different question without telling the reader.
Make the contract explicit
def summarize(values):
observed = [x for x in values if x is not None]
total = sum(observed)
return {
"observed": len(observed),
"missing": len(values) - len(observed),
"total": total,
"mean": total / len(observed) if observed else None,
}
result = summarize([0, 5, None, 15])
assert result["observed"] == 3
assert result["missing"] == 1
assert result["total"] == 20
assert result["mean"] == 20 / 3
assert summarize([])["mean"] is None
assert summarize([None, None])["missing"] == 2
Here an empty observed total is zero by arithmetic convention. The observed count and mean=None communicate that no observed mean exists. Do not display that zero as measured revenue without its context.
Decide what to do with text before arithmetic
A CSV cell may contain "0", an empty string, whitespace or "unknown". Check the field's rules before converting. A string "0" can represent a valid number; a blank may represent missing data. An unexpected word should usually produce a recorded validation issue, not silently become zero.
For monetary arithmetic, binary floating point may not match the required precision. Use a declared rounding policy and consider decimal arithmetic from strings. For general measurements, state units and sensible tolerances when checking results.
Keep a cleaning log
Record input rows, accepted rows, missing values, rejected values and duplicates. Check that these categories reconcile under your stated rules. If categories overlap, say so rather than adding them as though they were mutually exclusive.
An imputation rule introduces an assumption. Filling missing values with a median can be useful for a defined purpose, but the resulting distribution is not simply the original observed data. In a machine-learning workflow, fit any learned imputation values using training data only.
Try a meaningful extension
Add -5 and decide whether it is valid. A negative distance is probably invalid; a refund may be valid negative revenue. The correct rule comes from the field's meaning. Write the decision before editing the function, then add one test that would fail if the rule were broken.
Continue with Python for Data Science and Data Cleaning and Preparation. For file parsing details, consult the official Python CSV documentation.
