liveBook · Manning

Data cleaning

As explained in Starting Data Analytics with Generative AI and Python

A data cleaning protocol is a documented procedure for detecting, correcting, and documenting problems in data.

Data cleaning prepares data for analysis by detecting and correcting inaccuracies, inconsistencies, and formatting problems. A cleaning protocol documents the procedure and its treatment of problems. Like preparing ingredients before cooking, cleaning removes defects and makes inputs more consistent before they affect the result. section 2.4.1 section 2.0

Clean data as early as possible: representation and consistency errors can carry through preparation and distort later conclusions. The aim is to describe the business process rather than artifacts introduced during collection or preparation. section 2.5 section 2.4.1

A basic technical cleaning pass includes these steps:

Order matters. Filling missing values with a column average before removing duplicates can let repeated records influence that average. Inspect how the data changes after each step, because a transformation can hide an earlier error. Adapt the protocol to the data and use case rather than assuming one sequence fits everything. section 2.5 section 2.4

Cleaning reviews for keyword search

A search for positive customer reviews had about 89% specificity but only 47% sensitivity, missing many positives. An AI assistant suggested removing punctuation and extra whitespace, lowercasing, removing stopwords, and reducing words to roots. Choose steps for the actual dataset: lowercasing had already been applied, and not every suggestion was necessary. Reassess the search rather than assuming cleaning improved it. section 5.5.5

Listing 5.8 Data cleaning before keyword search
import string
import nltk
from nltk.stem import RSLPStemmer

nltk.download('rslp')
stemmer = RSLPStemmer()


def remove_punctuation(text):
    return text.translate(str.maketrans("", "", string.punctuation))

def remove_extra_whitespace(text):
        return ' '.join(text.split())

def stem_words(text):
        words = text.split()
    stemmed_words = [stemmer.stem(word) for word in words]
    return ' '.join(stemmed_words)

def clean_text(text):
        if not isinstance(text, str):
                 return ""
    text = text.lower()
    text = remove_punctuation(text)
    text = remove_extra_whitespace(text)
    text = stem_words(text)
    return text
df['cleaned_review_text'] = df['review_text'].apply(clean_text)

This Python listing combines lowercasing, punctuation and whitespace removal, and stemming before keyword search. section 5.5.5

Generative AI can suggest cleaning steps or code, but it may make hidden assumptions about the data. Check its suggestions against what is already known, and pair cleaning with visualization and data understanding before drawing conclusions. section 5.5.5 section 5.5.7

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Outlines a cleaning checklist and order-dependent risks, while leaving the protocol adaptable to the use case.
Examples
Uses duplicate removal before average imputation and Portuguese customer-review keyword matching to illustrate cleaning choices.
What it assumes
Assumes an overview of the data’s meaning and completeness, and emphasizes analytical experience when assessing AI suggestions.
What you can do after
Evaluate and apply cleaning steps to a dataset while checking their order and relevance.
How it connects
Connects cleaning with data quality assessment, exploratory data analysis, missing-value handling, and keyword search.

Also explained in

Compare how 14 books explain data cleaning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.