Data Collection, Preparation, Cleaning and Transformation
Plan a useful data collection process, recognise data quality problems, clean records and transform raw data into an analysis-ready dataset.
01
Learning objectives
Distinguish population, sample, observation and variable.
Identify missing, duplicate, inconsistent and invalid values.
Explain why collection method and sampling bias affect conclusions.
Describe cleaning and transformation steps before analysis.
Describe the independent, dependent and controlled variables in a simple experiment and explain why a control or baseline can be useful.
02
Lesson notes
Data analytics starts with a question, not with a chart. Define the population you want to understand, select an appropriate sample, choose variables and decide how measurements will be collected. A census measures every member of a population; a sample measures a subset. A poorly selected sample can produce biased results even when calculations are flawless.
Data quality includes accuracy, completeness, consistency, validity and timeliness. A dataset may contain missing values, duplicated rows, inconsistent units, spelling variations, impossible dates or values outside the accepted range. Cleaning identifies these issues and applies documented rules instead of silently deleting inconvenient observations.
Transformation makes data suitable for analysis. Typical steps include parsing dates, standardising units, trimming text, converting numeric fields, deriving new variables and grouping categories. Keep the raw source unchanged where possible, record each transformation and retain enough information to reproduce the process.
The handling of missing or unusual values depends on their cause and the research question. Deleting every incomplete row can bias results if missingness is not random. Replacing values with a mean can hide variation. Record the chosen policy, inspect the effect and communicate limitations with the final result.
Experimental design plans how an investigation will test a question. Identify the research question, independent variable (the factor deliberately changed), dependent variable (the outcome measured), controlled variables (factors kept as consistent as possible), participants or sample, procedure and method of recording results. A control group or baseline can help compare outcomes. A fair experiment changes the factor being studied while keeping other important conditions consistent. Repeat measurements where practical, document limitations and avoid claiming causation from a simple observational survey.
03
Worked code examples
Worked example
The transformation trims whitespace, removes the repeated row and flags a non-numeric age for review rather than treating it as a number.
raw = [" Ali , 18 ", "Sara,17", "Ali, 18", "Hina,not-known"]
clean = []
for row in raw:
name, age = [part.strip() for part in row.split(",")]
if age.isdigit():
clean.append({"name": name, "age": int(age)})
print(clean)