Data Cleaning

Python · Data Cleaning

Theory

Data cleaning fixes problems in raw data before analysis, and missing values are the first problem to tackle. pandas marks them as NaN (numbers), None or NaT (dates); df.isna() gives True where a value is missing and df.isna().sum() counts them per column. You then either fill or drop them.

fillna(0) fills every gap with one value; fillna({'city': 'Unknown', 'amount': 0}) uses a different value per column; fillna(df['amount'].median()) fills with a statistic. df.ffill() copies the last known value forward and df.bfill() copies the next one back - useful for time series and for 'carry the last price'. dropna() removes rows with any NaN, dropna(subset=['amount']) only checks some columns, dropna(thresh=2) keeps rows with at least 2 real values and dropna(how='all') drops rows that are completely empty. interpolate() estimates numeric gaps from neighbours, e.g. 100, NaN, 300 becomes 100, 200, 300.

Why we use it

Missing values quietly change results: mean() skips them, sum() treats them as 0, joins and groupbys may drop them, and many ML models refuse NaN. Handling them deliberately keeps totals and averages honest.

When to use it

Fill when a sensible default exists (0 orders, 'Unknown' city, last known price); drop when the row is useless without the value; interpolate for evenly spaced numeric readings. Always count NaN first so you know how much data a choice affects.

How it works

  1. Load raw data
  2. Count gaps with df.isna().sum()
  3. Is the row useless without this value? Yes: dropna(subset=[col]). No: Keep the row and fill.
  4. Sensible default exists? Yes: fillna(value), ffill() or interpolate(). No: Leave as NaN.
  5. Re-check isna().sum()
  6. DataFrame with missing values handled

Syntax

df.isna().sum()
df.fillna(value | {'col': value})
df.ffill(); df.bfill()
df.dropna(subset=[...], thresh=n, how='any'|'all')
s.interpolate()

Example

df = df.fillna({'city': 'Unknown', 'amount': 0})
df = df.dropna(subset=['name'])

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic