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:
- Remove duplicate records. section 2.4.1
- Handle missing values, considering the risks of estimating them. section 2.4.1 section 2.0
- Correct data entry errors and standardize inconsistent formats. section 2.4.1
- Validate the data before relying on it for analysis. section 2.4.1
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













