No history yet

Data Preparation

Beyond Basic Cleaning

You already know that clean data is essential. But professional data preparation goes beyond deleting empty rows or fixing typos. It’s about transforming raw, messy data into a high-quality asset ready for sophisticated analysis. This is how you avoid the classic 'garbage in, garbage out' trap, ensuring your insights are built on a solid foundation.

Data cleaning and preprocessing are perhaps the most time-consuming tasks in data analysis but are crucial for delivering accurate results.

Diagnosing Missing Data

When data is missing, the first step isn't to fill it, but to understand why it's missing. The reason tells you how to fix it without introducing bias into your analysis. There are three main patterns of missingness.

PatternFull NameDescriptionBusiness Example
MCARMissing Completely at RandomThe fact that a value is missing has no connection to any other data, observed or not.A customer accidentally skips a non-mandatory field on a sign-up form.
MARMissing at RandomThe missingness is related to another piece of observed data.In a sales dataset, 'commission amount' is often missing for deals closed by junior reps, but not senior reps. The missingness depends on 'employee seniority', which is recorded.
MNARMissing Not at RandomThe missingness is related to the missing value itself.Customers with very low satisfaction scores are the most likely to not fill out the 'customer satisfaction' field in a feedback survey.

Identifying the likely pattern is crucial. For MCAR, simply deleting the rows might be acceptable if you have a large dataset. But for MAR and MNAR, deletion can skew your results. Instead, we use more intelligent techniques to fill in the gaps.

Intelligent Imputation

Imputation is the process of replacing missing data with substituted values. While simple methods like using the mean or median are quick, they can distort the natural variance in your data. More advanced methods preserve the underlying relationships between your variables.

K-Nearest Neighbors (KNN) Imputation looks for the 'k' most similar complete records in your dataset—the 'neighbors'—and uses their values to estimate the missing one. For a numerical value, this might be the average of the neighbors. For a categorical value, it would be the most common value. This works well because it uses the local data structure to make an informed guess.

Multiple Imputation is an even more robust technique. Instead of creating one best guess, it creates several (e.g., 5 or 10) plausible imputed datasets. Each dataset is then analysed separately, and the results are pooled together. This process accounts for the uncertainty around the true value of the missing data, often leading to more accurate and reliable final conclusions.

Finding Duplicates in Disguise

Simple deduplication removes exact matches. But what about records for 'Smith, John', 'John Smith', and 'J. Smith Inc.'? These likely refer to the same entity but would be missed by standard methods. This is where fuzzy matching comes in.

Fuzzy matching algorithms don't look for perfect equality. Instead, they calculate a similarity score between strings. For example, the Levenshtein distance measures the number of edits (insertions, deletions, or substitutions) needed to change one word into another. You can then set a threshold—say, a similarity score of 90% or higher—to flag records as probable duplicates for manual review or automatic merging.

This technique is invaluable for merging customer databases from different sources, cleaning up supplier lists, or consolidating product catalogues where naming conventions vary.

Validation and Repair

Data should conform to a strict set of rules, known as a schema. Schema validation is the process of checking if the data meets these rules. This is more than just checking data types; it's about structural integrity.

Validation RuleProblem ExampleRepair Strategy
FormatDates stored as '12-05-2023', 'May 12, 2023', and '2023/05/12'.Standardise all dates to ISO 8601 format (YYYY-MM-DD).
RangeA customer age is listed as 150.Flag as an error. Cap values at a reasonable maximum or treat as a missing value.
Set MembershipA 'Country' field contains 'USA', 'U.S.A.', and 'United States'.Map all variations to a single standard, like the ISO 3166 code 'USA'.
ConsistencyA customer's 'Signup Date' is later than their 'First Purchase Date'.Flag the record for manual review to determine the correct chronological order.

Handling Outliers

Outliers are data points that differ significantly from other observations. They can be legitimate, but they can also be errors that skew your analysis, particularly statistical models like linear regression. Two common methods for detecting them are Z-score and the Interquartile Range (IQR).

Z-score measures how many standard deviations a data point is from the mean. A common rule of thumb is to flag any point with a Z-score greater than 3 or less than -3 as an outlier.

Z=xμσZ = \frac{x - \mu}{\sigma}

The Interquartile Range (IQR) method is more robust to the presence of outliers themselves. It focuses on the middle 50% of the data.

  1. Find the first quartile (Q1), the 25th percentile.
  2. Find the third quartile (Q3), the 75th percentile.
  3. Calculate the IQR: IQR=Q3Q1IQR = Q3 - Q1.
  4. Define the 'fences': The lower fence is Q11.5×IQRQ1 - 1.5 \times IQR, and the upper fence is Q3+1.5×IQRQ3 + 1.5 \times IQR.

Any data point that falls outside these fences is considered an outlier. Once identified, outliers aren't always deleted. They can be capped (set to the value of the fence), transformed (e.g., using a logarithm), or simply noted before proceeding with analysis.

Let's test your understanding of these data preparation techniques.

Quiz Questions 1/5

A survey asks about personal income, and you notice that individuals with very high incomes are significantly less likely to answer that specific question. What type of missing data pattern does this scenario most likely represent?

Quiz Questions 2/5

You need to find duplicate customer entries in a database, but names are entered inconsistently (e.g., 'John Smith', 'Smith, John', 'J. Smith'). Which technique is best suited for this task?

Mastering these preparation steps ensures the data you analyse is not just clean, but a truly reliable and powerful asset for decision-making.