L6 Your Data Is Already Messy

DATA SCIENCE · CYCLE 1 · L6 · TEXTBOOK CHAPTER

Your Data Is Already Messy

Something in your sheet is wrong. Every dataset looks this way before someone cleans it. You keep the record as it is, fix a copy, and log every change with its reason.

After this chapter, you can

Why It Matters

A wrong number still looks like a number. That is true of your own log. It is also true of a file a state publishes.

VDOT estimates the traffic on each section of road: the average number of vehicles a day, over a year. One section is I-95 southbound on the Capital Beltway, in the City of Alexandria. In ten of the eleven years from 2015 to 2025, the files give it between 56,000 and 78,000 vehicles a day.

The 2022 file gives it 217,386. That is more than three times the 64,000 of the year before and the 64,000 of the year after.

Line chart of vehicles a day on I-95 southbound at the Capital Beltway, 2015 to 2025. Ten years sit between 56,000 and 78,000. The point for 2022 is 217,386, and its route label has no direction.
Source: VDOT annual average daily traffic files, 2015 to 2025, Link ID 190002, from data.virginia.gov.

Read the chart as it is, and traffic more than tripled in 2022, then fell back in 2023. That did not happen.

The clue is in a text column. Every other year labels the route I-95 SB, for southbound. The 2022 file says I-95.

That year, the row counts the whole roadway. Four rows of the 2022 file hold the same 217,386: the lanes going north, the lanes going south, and the express lanes each way. A second column, AADT Dir, gives each row a figure for its own direction: 60,262, 66,331, 46,659 and 44,134. Those four add to 217,386. The southbound figure, 66,331, fits the other years.

A definition changed for one year, and the number still looked like a number. =MAX on the eleven years finds it in one step. Reading the label column says why. This chapter shows you how to find problems like this one in your own data, and how to fix them so that anyone can check your work.

The Idea

Keep RAW, clean the copy

RAW is the record: nothing already in RAW is ever changed. Every fix happens on CLEAN, one CLEANING LOG row per fix.

Three words (Virginia’s standard DS.7)
Validone consistent structure, within a sensible range
Completeno required value missing
Uniqueno duplicates, no extra entries
Four defects, in this order
1missing observations
2inconsistent category labels
3a definition or unit that drifted
4values that cannot be right

This is the third of the six stages of the data cycle, Data Processing. Typing a value into RAW is Data Acquisition and Collection. Any change after that, made on CLEAN with a log row, is Data Processing.

Two Tabs, Before Anything Else

Your raw data is the only record of what you observed. Type over a cell and that observation is lost. So before you fix anything, make two tabs.

  1. Right-click the tab at the bottom of your sheet > Duplicate.
  2. Right-click the original tab > Rename > RAW.
  3. Right-click the copy > Rename > CLEAN.

Each new row goes into RAW the night you take it, then gets copied to CLEAN. Nothing already in RAW is ever changed. Every fix happens on CLEAN and gets a row in the CLEANING LOG.

The Four Defects, and the Word Each Breaks

Here is a sample sheet. Somebody says they tracked sleep, a workout and screen time before bed. It has all four defects, and one more thing.

ABCD
1DateSleep (hours)ActivityScreen time (min)
2Aug 247.5Running45
3Aug 258run30
4Aug 266.560
5Aug 2770Run55
6Aug 287rest40
7Aug 29Jog35
8Aug 309none20
9Aug 318.5Running25
10Sep 17lifting90
11Sep 26Lifting1.5
12Sep 37.5run1
13Sep 37.5run1

Virginia has three words for whether data can be trusted.

DefectIn the sample sheetWord it breaks
1A missing observation: a day you meant to record has no value.B7 and C4 are blank.complete
2An inconsistent category label: one thing spelled several ways.C2:C13 holds eight spellings of three activities: Run, Lifting and Rest.valid
3A definition or unit that drifted: partway through, a number changed meaning.D11:D13 switches from minutes to hours.valid
4A value that cannot be right.B5 says 70 hours of sleep in a day that has 24.valid
A duplicate. Look for it last.Rows 12 and 13: Sep 3 was entered twice.unique

Drift is THE THING or its unit changing partway through collection. It breaks valid, because the column stops being stored in one unit. Each drifted value looks ordinary, so a range check can pass it.

A value that could be right but sits far from the rest is an outlier. Check it against your notes. Then keep it or fix it, and log why.

The Cleaning Log

Every change gets a row in the cleaning log. That includes the changes you are sure about.

  1. Open the CLEANING LOG template from Canvas.
  2. Right-click its tab > Copy to > Existing spreadsheet > your project file.
  3. In your file, right-click the new tab > Rename > CLEANING LOG.

The log has seven columns: date logged, row or cell, column, what it said, what I changed it to, why, how. How is the Sheets step, so someone else could repeat your cleaning. Why is the column that matters most. The reason shows a reader that you made a decision.

Here are two of the template’s example rows, from a log of water.

Date loggedRow / cellColumnWhat it saidWhat I changed it toWhyHow
9/17/2026row 4Water (bottles)33333 bottles in one day is not possible. Every other day is 2 to 4. Most likely an extra keystroke. The 3 is my inference from the other days.Typed in the cell
9/22/2026row 13Water (bottles)(blank)(left blank)I did not record Sunday 20 September and cannot reconstruct it. Left blank so AVERAGE skips it and nothing is guessed.No change; typed missed in Notes

Here is the test of a log. Someone asks next month why a row is gone. Can you answer from the log alone? A row deleted with a logged reason is fine. A row deleted with no reason is a small data fraud. Cleaning makes the data honest, and the log is the proof.

A Range Check: MIN, MAX, COUNT, COUNTA

A cell that starts with = is an instruction to the spreadsheet. Type it, press Return, and the cell shows the answer. B2:B13 means every cell from B2 down to B13.

  1. Click the CLEAN tab.
  2. Click an empty cell to the right of your data, such as F2.
  3. Type: =MIN(B2:B13)
  4. Press Return.

Then run =MAX, =COUNT and =COUNTA on the same range.

FormulaWhat it showsWhat it catches
=MIN, =MAXthe smallest and the largest valuea value outside a sensible range
=COUNThow many cells hold numbersbelow =COUNTA: text sits in a number column
=COUNTAhow many cells are not emptybelow your rows of data: you have blanks

Say the range that is possible for you before you look at MIN and MAX. Checking source data like this before you change anything is what Virginia’s standards call data validation.

Check the Date column too. Type dates as 9/9/2026. A real date sits at the right of its cell. If it sits at the left, Sheets has read it as text.

Find and Replace, and the Trap

To see every spelling in a column, sort the whole block by that column, A to Z, and read it top to bottom. Each spelling sits next to the ones like it. Then press Command-Z until the rows come back. Pick one spelling. Then:

  1. Click the CLEAN tab.
  2. Edit > Find and replace.
  3. Type the old spelling in Find and your spelling in Replace with.
  4. Set Search to This sheet.
  5. Tick Match entire cell contents.
  6. Replace all. One Replace all is one row in the CLEANING LOG.

Setting Search to This sheet keeps RAW untouched. Sheets starts on All sheets, which would rewrite RAW too.

The trap. Find walk, replace with Walking, and leave Match entire cell contents unticked. A cell that already said Walking becomes Walkinging. Sheets replaces the text wherever it appears, inside longer words too. Tick the box, every time.

Cell beforeBox untickedBox ticked
walkWalkingWalking
WalkingWalkingingWalking
walkedWalkingedwalked

Sheets ignores capital letters unless Match case is ticked. So walk finds the Walk inside Walking.

Sort, select (choose columns), filter and replace are the four ways to wrangle data in Virginia’s standards.

Try It: Clean Column C

The lab holds column C of the sample sheet. Find and replace works here as it does in Sheets, with Match case unticked. Meet the three goals.

Worked Examples

Example 1 · Find every defect. Check the whole sample sheet. Then give its cleaning log rows.

Create a strategy

Run MIN, MAX, COUNT and COUNTA on each column before you read any cell. Then look for the four defects in order. Read the Date column for duplicates last. Every change becomes one log row with a reason.

Apply the idea
Before any fixB, sleepC, activityD, screen
=MIN601
=MAX70090
=COUNT11012
=COUNTA111112
Rows of data121212

MAX catches the 70. COUNTA of 11 against 12 rows shows one blank each in B and C. Column C holds words, so its COUNT is 0. Column D passes every check.

Missing: C4 and B7 are blank. Labels: sorted A to Z, column C puts Jog, lifting and Lifting, none, rest, and run, Run and Running side by side. That is 8 labels and a blank for 3 activities. Drift: D11 to D13 read 1.5, 1 and 1 after a column of 20 to 90. Cannot be right: B5 says 70. Duplicate: rows 12 and 13 are the same in every column.

Date loggedRow / cellColumnWhat it saidWhat I changed it toWhyHow
9/22/2026B5Sleep (hours)707.0A day has 24 hours, and every other night is 6 to 9. Most likely a missed decimal point. The 7.0 is my inference from the other nights.Typed in the cell
9/22/2026rows 2, 9ActivityRunningRunSame activity, two spellings. Chose Run as the standard.Edit > Find and replace, This sheet, Match entire cell contents
9/22/2026D11, D12Screen time (min)1.5, 190, 60Recorded in hours from Sep 2. The column is minutes. 1.5 h = 90 min, and 1 h = 60 min.Typed in the cell
9/22/2026row 13allSep 3, 7.5, run, 1deletedThe same as row 12 in every column. Entered twice.Right-click row 13 > Delete row

Five more Replace all rows like the second one, for run, Jog, rest, none and lifting, bring column C to Run, Lifting and Rest.

Reflect and check

Two merges are judgments, Jog into Run and none into Rest, and the log says so. The drift in column D passed every formula: MIN 1, MAX 90, COUNT and COUNTA both 12. Reading the column top to bottom found it. Ask whether day 1 and day 10 mean the same thing. The duplicate is deleted last, so every row number already in the log still matches RAW.

Example 2 · The mean, before and after. Find the mean of the sleep column before and after cleaning. How far did one bad cell move it?

Create a strategy

=AVERAGE skips a blank and keeps everything else. So the raw column gives 11 values, the 70 and the duplicate among them. After cleaning there are 10 values. Add each list and divide by its count. Then give the percent change.

Apply the idea

Before: 7.5 + 8 + 6.5 + 70 + 7 + 9 + 8.5 + 7 + 6 + 7.5 + 7.5 = 144.5, over 11 values. 144.5 ÷ 11 = 13.14, about 13.1 hours.

After: 7.5 + 8 + 6.5 + 7 + 7 + 9 + 8.5 + 7 + 6 + 7.5 = 74, over 10 values. 74 ÷ 10 = 7.4, so 7.4 hours.

Change: (7.4 − 13.1) ÷ 13.1 × 100 = −43.5. The mean fell by about 44%.

Two bar charts of the sleep column on the same axis. On RAW, one bar reaches 70 and the mean line is at 13.1 hours. On CLEAN, every bar is between 6 and 9 and the mean line is at 7.4 hours.
The sleep column of the sample sheet, before and after cleaning. A made-up example. Both charts use the same axis, so the 70 shows its size.
Reflect and check

Ten of the eleven raw values sit between 6 and 9, so 13.1 should look wrong before any arithmetic. The 70 caused almost all of the change. The sum fell by 70.5: reading the 70 as 7.0 removed 63, and the duplicate removed 7.5. Deleting the 70 gives 67 ÷ 9 = 7.44, the same to one decimal place. Either choice is fair once it is in the log.

Example 3 · Blank or zero? Two cells are blank: B7 (sleep on Aug 29) and C4 (activity on Aug 26). Decide what to do with each.

Create a strategy

Ask the one question that separates blanks: did I forget to record it, or did nothing happen that day? A blank means unknown. A zero, or a label that means none, means measured, and it was nothing. =AVERAGE skips a blank and counts a zero.

Apply the idea

B7. The person slept on Aug 29, so a true value exists and nobody recorded it. The cell stays blank and gets a log row that says so. Filling it with a guess would be inventing data.

C4. The person who collected the data has to remember. If no workout happened that day, the honest entry is the label for no workout, Rest. No workout is a real observation. If they cannot remember, the cell stays blank and the log says it cannot be recovered.

Reflect and check

Suppose you fill B7 with 7 so the column looks full. The mean becomes (74 + 7) ÷ 11 = 7.36, still 7.4 to one decimal place. The number barely moves, and it is still wrong: the 7 is a value you reported that nobody measured. Decide blanks while you still remember the week.

The Same in Python

Python comes in L19b, in Cycle 2. Once you have read it, come back and run the COUNT check here, on six cells from a messy screen time column.

In Sheets this is: counting the rows of the column, then =COUNT on it.

In English:

  1. Get the pandas toolbox and call it pd.
  2. Make a column of the six things typed in the cells, as text, and call it raw.
  3. Keep the numbers in raw; anything else becomes blank. Call the result screen.
  4. Print how many rows raw has.
  5. Print how many numbers screen has.

Predict: one cell says 60 min and one is empty. What will line 5 print? Then change "60 min" to "60", say the new count, and run it again.

Can’t run it here? Show what it prints
6 rows 4 numbers

The gap, 6 − 4 = 2, is the 60 min and the empty cell together. The 1.5 is counted as a number. It is in hours, and no count can tell you that. Reading the column does.

Want to type your own? The Python practice page has tasks and a notebook.

In your project

Your Cycle 1 project is your own data, collected over about a month, in one Google Sheet. The Sheet has a RAW tab, a CLEAN tab, a CLEANING LOG tab and an ABOUT tab. Each new row goes into RAW, then gets copied to CLEAN. Every chart, and every number in your one-page Google Doc, comes from CLEAN.

The Data Processing row of the Data Cycle Rubric asks for each problem named by type, its fix stated, and the raw and cleaned versions both visible. It also asks that the data is assessed for validity, completeness and uniqueness. Your RAW, CLEAN and CLEANING LOG tabs show a reader all of that. They also answer the question a reader asks of part 2 of your Doc: can I trust it?

Cell A1 of the CLEANING LOG tab names the change that most affected your results, with the number before and after. The sample sheet’s would be: Changing B5 from 70 to 7.0 moved the mean sleep from 13.1 hours to 7.4 hours.

Check Yourself

Answer each one before you look. The first two are from L5 and L4, so you keep them.

A sheet holds Date in column A and Steps in column B. You sort column B alone, Z to A. What happens? L5

A sheet has one row for each day from 3 March to 20 March. How many rows of data is that? L4

A column holds quiz scores out of 20. One cell says 200. Which word does that cell break?

A column should hold 24 values, in B2 to B25. =COUNT(B2:B25) gives 20. =COUNTA(B2:B25) gives 23. How many filled cells hold text?

You run Find bike, Replace with Biking. Match entire cell contents is unticked. What does the cell biked become?

Minutes of homework on six nights: 40, 35, 450, 50, 45 and 40. The student’s planner says 45 for the third night. On CLEAN they type 45 and log it. What is the mean now, in minutes?

A column holds 30, 60, a blank cell, 90 and 60. What does =AVERAGE show?

A header says Practice (min). The column reads 30, 45, 60, then 1.5 and 0.5. =COUNT and =COUNTA both give 5. What most likely happened?

Last week you typed 12 for a night you slept 7 hours. Your watch app still shows 7. Where do you fix it?

Read More

The Quartz guide to bad data. A free list of the problems reporters meet in real data, from the news site Quartz. Read five of its first eight: Values are missing, Zeros replace missing values, Rows or values are duplicated, Spelling is inconsistent, and Units are not specified. They are this chapter’s blank, zero, duplicate, labels and drift.

Stuck on any of this? Ask Mr. Case in class or send him a message in Canvas. Name the example or the question number, and say which step lost you.