惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Project Zero
Project Zero
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
爱范儿
爱范儿
P
Proofpoint News Feed
F
Fortinet All Blogs
雷峰网
雷峰网
小众软件
小众软件
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Secure Thoughts
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
S
Security @ Cisco Blogs
Help Net Security
Help Net Security
GbyAI
GbyAI
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
B
Blog
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
H
Heimdal Security Blog
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Help Net Security
O
OpenAI News
H
Hacker News: Front Page
博客园 - 叶小钗
Last Week in AI
Last Week in AI
S
Schneier on Security
The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
MyScale Blog
MyScale Blog
Recorded Future
Recorded Future
博客园 - 【当耐特】
V
Vulnerabilities – Threatpost
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
The Hacker News
The Hacker News
A
Arctic Wolf

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Dirty Data: How to Find It and What to Do
Akhilesh · 2026-04-26 · via DEV Community

You loaded your data. You ran head(). Everything looks fine.

It is not fine.

The data that looks fine in head() hides its problems. The missing values are three thousand rows down. The duplicates are in the middle. The date column that looks like a date is actually a string and will break your model silently. The salary column has a value of negative forty thousand that nobody caught.

Every real dataset has these problems. Every single one without exception. The question is not whether your data is dirty. The question is whether you found the dirt before you built a model on top of it.

This post is about finding it. Systematically. All of it.


The Dataset We Are Working With

Create this CSV first so you have something realistic to clean.

import pandas as pd
import numpy as np

data = {
    "id":         [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10],
    "name":       ["Alex", "priya", "SAM", "Jordan", None, "Jordan", "Lisa", "Ravi ", "Tom", "Nina", "Oscar"],
    "age":        [25, 30, 22, 35, 28, 35, -5, 150, 31, 29, 27],
    "salary":     [55000, 82000, None, 95000, 67000, 95000, 48000, 71000, None, 63000, 59000],
    "join_date":  ["2022-01-15", "2021-03-22", "2023-06-01", "20/04/2019",
                   "2022-11-30", "20/04/2019", "2021-07-14", "invalid_date",
                   "2023-02-28", "2022-08-10", "2021-12-05"],
    "department": ["Eng", "Marketing", "eng", "Sales", "Marketing",
                   "Sales", "Eng", "Engineering", "Eng", "marketing", "Sales"]
}

df = pd.DataFrame(data)
df.to_csv("messy_data.csv", index=False)
df = pd.read_csv("messy_data.csv")
print(df)

Enter fullscreen mode Exit fullscreen mode

Look at what is wrong in this data. Duplicate rows (row 3 and row 5 are the same person). Missing values in name, salary. Negative age (-5). Impossible age (150). Inconsistent department names (Eng, eng, Engineering, marketing, Marketing). Inconsistent date formats. A trailing space in "Ravi ". An invalid date string.

This is mild compared to real datasets.


Step 1: The Full Audit

Before fixing anything, see everything that needs fixing.

print("SHAPE:", df.shape)
print("\nDTYPES:")
print(df.dtypes)

print("\nMISSING VALUES:")
missing = df.isnull().sum()
missing_pct = (df.isnull().sum() / len(df) * 100).round(1)
missing_report = pd.DataFrame({"count": missing, "percent": missing_pct})
print(missing_report[missing_report["count"] > 0])

print("\nDUPLICATE ROWS:", df.duplicated().sum())

print("\nUNIQUE VALUES PER COLUMN:")
for col in df.columns:
    print(f"  {col}: {df[col].nunique()} unique values")

Enter fullscreen mode Exit fullscreen mode

Output:

SHAPE: (11, 6)

DTYPES:
id             int64
name          object
age            int64
salary        float64
join_date      object
department    object

MISSING VALUES:
        count  percent
name        1      9.1
salary      2     18.2

DUPLICATE ROWS: 1

UNIQUE VALUES PER COLUMN:
  id: 10 unique values
  name: 10 unique values
  age: 11 unique values
  salary: 9 unique values
  join_date: 10 unique values
  department: 6 unique values

Enter fullscreen mode Exit fullscreen mode

One duplicate row. Missing values in name and salary. Department has 6 unique values but should probably have 3. Already suspicious.


Step 2: Duplicate Rows

Always handle duplicates before anything else. Cleaning duplicates after other fixes is harder.

print("Duplicate rows:")
print(df[df.duplicated(keep=False)])

df = df.drop_duplicates()
print(f"\nAfter removing duplicates: {df.shape}")

Enter fullscreen mode Exit fullscreen mode

Output:

Duplicate rows:
   id    name  age   salary   join_date department
3   4  Jordan   35  95000.0  20/04/2019      Sales
5   4  Jordan   35  95000.0  20/04/2019      Sales

After removing duplicates: (10, 6)

Enter fullscreen mode Exit fullscreen mode

keep=False shows you all duplicated rows, not just the second occurrence. Good for verifying what you are about to delete.

Sometimes duplicate IDs are the issue, not duplicate rows. A row might have the same ID but different values, which means data corruption or a merge that went wrong.

duplicate_ids = df[df["id"].duplicated(keep=False)]
if len(duplicate_ids) > 0:
    print("Rows with duplicate IDs:")
    print(duplicate_ids)

Enter fullscreen mode Exit fullscreen mode


Step 3: Missing Values

You saw the counts. Now decide what to do with each one.

print("Rows with any missing value:")
print(df[df.isnull().any(axis=1)])

Enter fullscreen mode Exit fullscreen mode

Output:

    id  name  age   salary   join_date department
2    3   SAM   22      NaN  2023-06-01        eng
4    5  None   28  67000.0  2022-11-30  Marketing
7    8  Ravi   31      NaN  2023-02-28        Eng

Enter fullscreen mode Exit fullscreen mode

Three options for each missing value: drop the row, fill with a calculated value, fill with a placeholder.

df["salary"] = df["salary"].fillna(df["salary"].median())

df["name"] = df["name"].fillna("Unknown")

print("\nMissing values after filling:")
print(df.isnull().sum())

Enter fullscreen mode Exit fullscreen mode

Output:

Missing values after filling:
id            0
name          0
age           0
salary        0
join_date     0
department    0
dtype: int64

Enter fullscreen mode Exit fullscreen mode

Why median for salary and not mean? Because salary distributions are skewed. A few very high earners inflate the mean. Median is more representative of a typical value.


Step 4: Impossible Values

Missing values are marked. Impossible values are hiding as normal numbers.

print("Age distribution:")
print(df["age"].describe())

print("\nSuspicious ages:")
print(df[(df["age"] < 16) | (df["age"] > 100)][["id", "name", "age"]])

Enter fullscreen mode Exit fullscreen mode

Output:

Age distribution:
count    10.000000
mean      42.800000
std       36.824...
min       -5.000000
25%       27.250000
50%       30.000000
75%       31.500000
max      150.000000

Suspicious ages:
   id  name  age
6   6  Lisa   -5
7   7  Ravi  150

Enter fullscreen mode Exit fullscreen mode

Mean of 42.8 when most values cluster around 25-35 already tells you something is off. The min of -5 and max of 150 confirm it.

age_median = df[(df["age"] >= 16) & (df["age"] <= 100)]["age"].median()

df.loc[(df["age"] < 16) | (df["age"] > 100), "age"] = age_median
print(f"Replaced impossible ages with median: {age_median}")
print(df[["id", "name", "age"]])

Enter fullscreen mode Exit fullscreen mode

Replace with median of the valid values only. Using the contaminated median would pull the central value toward the impossible numbers.


Step 5: Inconsistent Strings

The department column has six unique values but should have three.

print("Raw department values:")
print(df["department"].value_counts())

Enter fullscreen mode Exit fullscreen mode

Output:

Eng            4
Marketing      3
Sales          2
eng            1
Engineering    1
marketing      1

Enter fullscreen mode Exit fullscreen mode

Three real departments written six different ways. Strip whitespace, lowercase everything, then map to canonical names.

df["name"] = df["name"].str.strip().str.title()
df["department"] = df["department"].str.strip().str.lower()

dept_map = {
    "eng": "Engineering",
    "engineering": "Engineering",
    "marketing": "Marketing",
    "sales": "Sales"
}

df["department"] = df["department"].map(dept_map)

print("Cleaned department values:")
print(df["department"].value_counts())

Enter fullscreen mode Exit fullscreen mode

Output:

Engineering    5
Marketing      3
Sales          2

Enter fullscreen mode Exit fullscreen mode

Always strip whitespace on string columns immediately after loading. Trailing spaces are invisible and cause "Ravi " != "Ravi" comparisons to fail silently.


Step 6: Date Columns

Pandas reads dates as strings unless you tell it otherwise. Mixed date formats are a common disaster.

print("Current join_date dtype:", df["join_date"].dtype)
print("Sample values:")
print(df["join_date"].head(5))

Enter fullscreen mode Exit fullscreen mode

Output:

Current join_date dtype: object
Sample values:
0    2022-01-15
1    2021-03-22
2    2023-06-01
3    20/04/2019
4    2022-11-30

Enter fullscreen mode Exit fullscreen mode

Two formats: YYYY-MM-DD and DD/MM/YYYY. One invalid string.

df["join_date"] = pd.to_datetime(df["join_date"], errors="coerce", dayfirst=False)

print("\nAfter conversion:")
print(df["join_date"].dtype)
print(df[["name", "join_date"]])

Enter fullscreen mode Exit fullscreen mode

Output:

After conversion:
datetime64[ns]

        name  join_date
0       Alex 2022-01-15
1      Priya 2021-03-22
2        Sam 2023-06-01
3     Jordan        NaT
4    Unknown 2022-11-30
...
7       Ravi        NaT

Enter fullscreen mode Exit fullscreen mode

errors="coerce" converts anything it cannot parse to NaT (Not a Time, the datetime equivalent of NaN). The invalid date and the misformatted date became NaT. Now you know exactly where the problems are.


Step 7: Data Type Fixes

print("Before dtype fixes:")
print(df.dtypes)

df["id"] = df["id"].astype(str)

df["salary"] = df["salary"].astype(int)

print("\nAfter dtype fixes:")
print(df.dtypes)

Enter fullscreen mode Exit fullscreen mode

Sometimes columns load as the wrong type because of mixed values. A column that should be integer loads as float because it had NaN values (NaN forces float). After filling the NaNs, convert back.


Step 8: Final Validation

After all cleaning, verify the result.

def final_check(df):
    print("=" * 50)
    print("FINAL DATA QUALITY REPORT")
    print("=" * 50)
    print(f"Shape: {df.shape}")
    print(f"Missing values: {df.isnull().sum().sum()}")
    print(f"Duplicates: {df.duplicated().sum()}")
    print(f"\nDtypes:")
    print(df.dtypes)
    print(f"\nNumerical column stats:")
    print(df.describe())
    print("=" * 50)

final_check(df)

Enter fullscreen mode Exit fullscreen mode

Run this after every cleaning operation. If anything unexpected appears, you catch it before it contaminates your analysis or your model.


The Thinking Behind Every Decision

Cleaning decisions are not mechanical. They require judgment.

When you drop a row versus fill a missing value, you are making a choice about what to assume. Dropping is safer but loses data. Filling is riskier but keeps samples.

When you replace an impossible age with the median, you are assuming the measurement was an error. But what if age 150 was a test record that should be deleted entirely?

When you standardize department names, you are assuming "eng" and "Engineering" mean the same thing. Usually true. Sometimes not.

Document every decision you make. Not for anyone else. For yourself, when you come back to this code in three months and wonder why you did what you did.


A Blog That Goes Deep on This

Chris Albon runs a site called Machine Learning Flashcards and has a well-known collection of practical data cleaning recipes at chrisalbon.com. His post on handling missing values in Pandas is one of the most referenced pieces on the topic. Very code-first, very practical. Search "Chris Albon pandas missing data" and it comes right up.

Towards Data Science published a piece by Jeff Hale called "The Ultimate Guide to Data Cleaning" that covers this topic across multiple tools and with real datasets. Widely shared in the data science community. Search "Jeff Hale ultimate guide data cleaning".


Try This

Create cleaning_practice.py.

Download the Titanic dataset from Kaggle (it is free, search "Titanic dataset Kaggle" and download the CSV). Load it with Pandas.

Do the full audit: shape, dtypes, missing values, duplicates.

Fix every problem you find:

  • Missing Age values: fill with median age grouped by Pclass (different passenger classes had different age distributions)
  • Missing Cabin values: fill with "Unknown"
  • Missing Embarked values: fill with the most common port
  • Check for impossible values in Fare and Age
  • Standardize any string columns that need it

After cleaning, verify with the final check function. The cleaned dataset should have zero missing values except possibly in columns you intentionally left.

Write a short comment above each cleaning step explaining why you made the choice you made.


What's Next

Your data is clean now. Next up is filtering and selecting specific rows and columns efficiently using loc and iloc. You have seen these briefly, but the next post goes deep on the patterns that come up constantly in real analysis.