Skip to main content
Xenors Xenors
XENORS
Home › Data Science › Data Cleaning in Python: Handling Missing Values & Outliers
Module lesson · 16 of 45

Data Cleaning in Python: Handling Missing Values & Outliers

Learn a practical Python data cleaning workflow for missing values, duplicates, inconsistent types, categories and outliers.

By Ashok Kumar YadavHands-on examples + practiceDataset + notebook included
Data Cleaning in Python: Handling Missing Values & Outliers

Learn a practical Python data cleaning workflow for missing values, duplicates, inconsistent types, categories and outliers. Use the dedicated Data Cleaning in Python dataset and notebook to reproduce the lesson rather than reading it passively. This page targets the practical intent behind the cleaning workflow, not repetition of the phrase itself.

Editorial note: Publication metadata for Data Cleaning in Python is intentionally unset until this URL is actually released; the lesson already includes its own examples, CSV and notebook.

Build the mental model behind Data Cleaning in Python

A customer-age column containing -1, 999 and text values should first be converted safely to numeric, then validated against a plausible range. Invalid values can be set to missing and handled according to the analysis goal.

  • Cleaning is not deleting inconvenient rows; it is making data quality decisions explicit and reproducible.
  • Missing values can mean unavailable, not applicable or genuinely absent, and those cases should not be treated identically.
  • Outlier handling depends on context: a rare but valid transaction is different from a sensor error.
  • Keep raw data immutable and record every cleaning rule so another analyst can reproduce the dataset.

Run small examples and inspect every intermediate object

For Data Cleaning in Python, predict one visible result before each snippet runs—a row count, shape, statistic, ranking or error pattern—then compare it with the printed checkpoint.

Example 1 · Baseline example
# Data Cleaning in Python · example 1
import pandas as pd

df['age'] = pd.to_numeric(df['age'], errors='coerce')
df.loc[~df['age'].between(0, 110), 'age'] = pd.NA
df = df.drop_duplicates(subset='customer_id')
print(df['age'].isna().mean())
Output / checkpoint
Data Cleaning in Python checkpoint 1: Run the cell and inspect the printed values before changing anything.
Example 2 · Inspect data before transforming it
# Data Cleaning in Python · example 2
import pandas as pd

df = pd.read_csv('practice.csv')
print(df.shape)
print(df.dtypes)
print(df.head(3).to_string(index=False))
Output / checkpoint
Data Cleaning in Python checkpoint 2: (30, 5)
Column types are printed, followed by the first three rows.
Example 3 · Check missing values explicitly
# Data Cleaning in Python · example 3
missing = df.isna().sum().sort_values(ascending=False)
print(missing[missing.gt(0)])
Output / checkpoint
Data Cleaning in Python checkpoint 3: Only columns with at least one missing value are listed.
Example 4 · Create a reproducible summary
# Data Cleaning in Python · example 4
summary = df.select_dtypes('number').agg(['count','mean','median','min','max']).T
print(summary.round(2))
Output / checkpoint
Data Cleaning in Python checkpoint 4: A compact numeric summary appears with one row per numeric column.
Example 5 · Test one edge case
# Data Cleaning in Python · example 5
test = df.copy()
if len(test):
    test.iloc[0, 0] = test.iloc[0, 0]
print('rows:', len(test), 'duplicates:', test.duplicated().sum())
Output / checkpoint
Data Cleaning in Python checkpoint 5: The row count and duplicate count make the check visible instead of implicit.

Debug shape, dtype and missing-value problems

Verification habit

Outlier handling depends on context: a rare but valid transaction is different from a sensor error.

Failure mode to watch

Keep raw data immutable and record every cleaning rule so another analyst can reproduce the dataset.

Self-check with answers

For Data Cleaning in Python, what result should you be able to explain without the code?

Cleaning is not deleting inconvenient rows; it is making data quality decisions explicit and reproducible.

Which assumption matters most in this Data Cleaning in Python exercise?

Outlier handling depends on context: a rare but valid transaction is different from a sensor error.

What failure mode would make this Data Cleaning in Python result untrustworthy?

Keep raw data immutable and record every cleaning rule so another analyst can reproduce the dataset.

Turn the lesson into a reusable notebook

Try it yourself workspace

Edit this Data Cleaning in Python starter, copy it, and run it in the downloadable notebook or your local Jupyter environment.

Questions specific to this lesson

What should I inspect before using Data Cleaning in Python?

Missing values can mean unavailable, not applicable or genuinely absent, and those cases should not be treated identically.

How do I debug a wrong result for Data Cleaning in Python?

Print shape, dtypes and a few representative rows after each transformation. That usually reveals whether the issue is selection, missing data, joins or implicit type conversion.

Should I prefer a one-line expression or several steps for Data Cleaning in Python?

Prefer the version you can test and explain. Compact code is useful only when the intermediate assumptions remain visible.

How can I practise this lesson for Data Cleaning in Python?

Download the page-specific CSV and notebook, reproduce the examples, then add one column or edge case and document how the output changes.

Primary references and further reading

For Data Cleaning in Python, use these primary references when exact syntax, defaults or formal definitions matter beyond the examples on this page.

AK
Written by Ashok Kumar Yadav

Ashok Kumar Yadav writes Xenors tutorials on applied AI, programming and data workflows. This Data Cleaning in Python lesson is designed as a reproducible learning exercise rather than a keyword-first article.

GitHub · LinkedIn

Continue exploring