Skip to main content
Workforce LibreTexts

4: Data Cleaning and Preparation

  • Page ID
    48045
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    Learning Objectives
    • Explain why data cleaning is essential in analytics, including the idea of “Garbage In, Garbage Out (GIGO)”.
    • Identify common data quality problems such as missing values, duplicates, formatting inconsistencies, invalid values, and type mismatches.
    • Distinguish between NULL, empty values, and valid zero/default values in a dataset.
    • Evaluate different strategies for handling missing data, including deletion, imputation, defaults, and flagging.
    • Differentiate required fields from optional fields and explain why that matters in data preparation.
    • Recognize the importance of correct data types and apply transformations to make data analysis-ready.
    • Use SQL functions such as CASTTRIMUPPERLOWERCOALESCE, and filtering queries to clean and transform data.
    • Explain the role of primary keys, unique constraints, and duplicate detection in maintaining clean datasets.
    • Detect and resolve duplicate records, including both exact duplicates and likely logical duplicates.
    • Apply valid range checks and business rules to determine whether data values make sense in context.
    • Explain data integrity concepts, including entity integrity, referential integrity, foreign keys, and check constraints.
    • Analyze data distributions to detect skewness, outliers, and possible data entry errors.
    • Compare SQL-based cleaning with visual tools such as Cloud Dataprep and Looker Studio.
    • Describe best practices for data cleaning, including profiling early, documenting changes, preserving raw data, validating results, and automating repeatable steps.
    • Prepare a dataset so it is accurate, consistent, complete, and ready for reliable analysis.

    • 4.1: Importance of Data Cleaning in Analytics
      This section establishes the foundational principle of "Garbage In, Garbage Out" and explains why data quality is critical to analytics success. It discusses how data professionals spend approximately 80% of their time on cleaning and preparation activities, explores the real-world consequences of poor data quality including financial and reputational costs, and introduces common data quality issues such as missing values, duplicates, formatting inconsistencies, and invalid values. The section e
    • 4.2: Handling Missing Data (NULL Values)
      NULL represents the absence of data, distinct from zero or empty strings. Missing data occurs due to data entry omissions, incomplete sources, or optional fields. Key strategies include: (1) Omission - deleting records with missing values when appropriate; (2) Imputation - filling missing values with estimates like mean, median, or predictive models; (3) Business-rule defaults - using domain-specific values; (4) Flagging - creating indicators for missingness patterns. Required fields should neve
    • 4.3: Data Types and Data Transformation
      Correct data types ensure meaningful storage and interpretation. Mismatched types (e.g., prices stored as text) prevent proper calculations, sorting, and filtering. SQL's CAST function converts between types, but validation is needed to catch conversion failures. Common transformations include: trimming whitespace, normalizing case, replacing/recoding values, splitting/merging fields, and deriving new calculated values. Tools like Cloud Dataprep automate type inference and suggest corrections.
    • 4.4: Uniqueness, Keys, and Duplicate Data
      This chapter addresses the critical issue of duplicate records, which can distort data analysis and compromise integrity. You will learn how to leverage primary keys, unique constraints, and various detection techniques to find and resolve data inconsistencies.
    • 4.5: Valid Ranges and Business Rules
      This chapter explains that data cleaning involves verifying data validity by checking against logical range constraints and domain-specific business rules. It outlines strategies for handling violations—such as correction, exclusion, or nullification—to ensure the dataset is not just technically correct but also logically consistent with the real world.
    • 4.6: Data Integrity and Constraints
      This chapter defines data integrity as the consistency, accuracy, and reliability of data, which is maintained through constraints like referential integrity (foreign keys), entity integrity, and check constraints. It stresses the importance of enforcing these rules early in the data pipeline to prevent bad data and reduce the cost of cleaning.
    • 4.7: Data Distribution and Skewness
      This chapter explains why analyzing data distribution and skewness is a crucial part of data preparation for identifying hidden problems like outliers, data entry errors, and other anomalies. It covers various methods for detecting these issues using visuals and summary statistics, and discusses strategies for handling skewed data to make it suitable for analysis.
    • 4.8: Tools and Techniques for Data Cleaning and Preparation
      This chapter serves as a practical guide to the tools and methods used for data cleaning and preparation, bridging the gap between theoretical concepts and their application in real workflows.
    • 4.9: Cleaning Data with SQL
      This chapter explains how to use SQL for various data cleaning tasks, including filtering to find anomalies, transforming data for consistency with functions like `UPDATE` and `TRIM`, and removing duplicates using window functions. It also covers creating repeatable cleaning workflows with scripts and validating data using database constraints.
    • 4.10: Best Practices in Data Cleaning and Preparation
      This chapter outlines essential best practices for data cleaning and preparation, emphasizing a systematic process that includes enforcing data quality at the source, documenting changes, and preserving raw data. Following these guidelines ensures the creation of reliable, high-quality datasets, which are the bedrock of trustworthy and insightful analysis.
    • 4.11: Key Terms
    • 4.12: Assessments
    • 4.13: Detailed Figure Captions - Chapter 4
    • 4.14: References


    This page titled 4: Data Cleaning and Preparation was last modified on Thu, 28 May 2026 15:17:30 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Felix Amoruwa.

    • Was this article helpful?