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
- Keep RAW untouched and clean only the CLEAN tab.
- Find the four defects and name the word each breaks: valid, complete or unique.
- Check a column with MIN, MAX, COUNT and COUNTA, and log every change with its why and how.
- Run Find and replace with Match entire cell contents ticked.
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.
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) |
| Valid | one consistent structure, within a sensible range |
| Complete | no required value missing |
| Unique | no duplicates, no extra entries |
| Four defects, in this order |
| 1 | missing observations |
| 2 | inconsistent category labels |
| 3 | a definition or unit that drifted |
| 4 | values 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.
- Right-click the tab at the bottom of your sheet > Duplicate.
- Right-click the original tab > Rename > RAW.
- 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.
| A | B | C | D |
| 1 | Date | Sleep (hours) | Activity | Screen time (min) |
| 2 | Aug 24 | 7.5 | Running | 45 |
| 3 | Aug 25 | 8 | run | 30 |
| 4 | Aug 26 | 6.5 | | 60 |
| 5 | Aug 27 | 70 | Run | 55 |
| 6 | Aug 28 | 7 | rest | 40 |
| 7 | Aug 29 | | Jog | 35 |
| 8 | Aug 30 | 9 | none | 20 |
| 9 | Aug 31 | 8.5 | Running | 25 |
| 10 | Sep 1 | 7 | lifting | 90 |
| 11 | Sep 2 | 6 | Lifting | 1.5 |
| 12 | Sep 3 | 7.5 | run | 1 |
| 13 | Sep 3 | 7.5 | run | 1 |
Virginia has three words for whether data can be trusted.
- Valid: every value is formatted and stored in one consistent structure, and within a sensible range.
- Complete: no required value is missing. Every day you meant to record has a value or a stated reason it does not.
- Unique: no duplicates and no extra entries. One observation entered twice breaks this. So does a practice row typed while setting up.
| Defect | In the sample sheet | Word it breaks |
| 1 | A missing observation: a day you meant to record has no value. | B7 and C4 are blank. | complete |
| 2 | An inconsistent category label: one thing spelled several ways. | C2:C13 holds eight spellings of three activities: Run, Lifting and Rest. | valid |
| 3 | A definition or unit that drifted: partway through, a number changed meaning. | D11:D13 switches from minutes to hours. | valid |
| 4 | A 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.
- Open the CLEANING LOG template from Canvas.
- Right-click its tab > Copy to > Existing spreadsheet > your project file.
- 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 logged | Row / cell | Column | What it said | What I changed it to | Why | How |
| 9/17/2026 | row 4 | Water (bottles) | 33 | 3 | 33 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/2026 | row 13 | Water (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.
- Click the CLEAN tab.
- Click an empty cell to the right of your data, such as F2.
- Type:
=MIN(B2:B13)
- Press Return.
Then run =MAX, =COUNT and =COUNTA on the same range.
| Formula | What it shows | What it catches |
=MIN, =MAX | the smallest and the largest value | a value outside a sensible range |
=COUNT | how many cells hold numbers | below =COUNTA: text sits in a number column |
=COUNTA | how many cells are not empty | below 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:
- Click the CLEAN tab.
- Edit > Find and replace.
- Type the old spelling in Find and your spelling in Replace with.
- Set Search to This sheet.
- Tick Match entire cell contents.
- 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 before | Box unticked | Box ticked |
| walk | Walking | Walking |
| Walking | Walkinging | Walking |
| walked | Walkinged | walked |
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 strategyRun 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 fix | B, sleep | C, activity | D, screen |
=MIN | 6 | 0 | 1 |
=MAX | 70 | 0 | 90 |
=COUNT | 11 | 0 | 12 |
=COUNTA | 11 | 11 | 12 |
| Rows of data | 12 | 12 | 12 |
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 logged | Row / cell | Column | What it said | What I changed it to | Why | How |
| 9/22/2026 | B5 | Sleep (hours) | 70 | 7.0 | A 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/2026 | rows 2, 9 | Activity | Running | Run | Same activity, two spellings. Chose Run as the standard. | Edit > Find and replace, This sheet, Match entire cell contents |
| 9/22/2026 | D11, D12 | Screen time (min) | 1.5, 1 | 90, 60 | Recorded 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/2026 | row 13 | all | Sep 3, 7.5, run, 1 | deleted | The 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 checkTwo 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%.
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 checkTen 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 strategyAsk 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 checkSuppose 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:
- Get the pandas toolbox and call it pd.
- Make a column of the six things typed in the cells, as text, and call it raw.
- Keep the numbers in raw; anything else becomes blank. Call the result screen.
- Print how many rows raw has.
- 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 sort of one column alone moves that column and leaves the rest. Only the steps move, and the dates stay where they were. Each step count now sits beside the wrong date, and nothing warns you. Always sort the whole block: select from A1, header row included, then Data > Sort range > Advanced range sorting options. To undo, press Command-Z until the rows come back.
A sheet has one row for each day from 3 March to 20 March. How many rows of data is that? L4
20 − 3 + 1 = 18. Both end days count, so you add 1. One row is one day, so 18 days make 18 rows of data. A missed day keeps its row: blank cells, and missed in Notes.
A column holds quiz scores out of 20. One cell says 200. Which word does that cell break?
Valid means every value is stored in one consistent structure, and within a sensible range. A score out of 20 cannot be 200. Nothing is missing, so the column is complete. No row is entered twice, so it is unique. Check the 200 against the record: it may be a 20 with an extra keystroke. Fix it on CLEAN, and log why.
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?
23 − 20 = 3. COUNTA counts the cells that are not empty, and COUNT counts the cells that hold numbers, so 3 filled cells hold text. Also, 24 − 23 = 1 cell is blank. The counts cannot say which cells, or why the blank is blank. Read the column to find out.
You run Find bike, Replace with Biking. Match entire cell contents is unticked. What does the cell biked become?
Sheets replaces the text wherever it appears, inside longer words too. It finds bike inside biked and puts Biking there. The d stays: Bikingd. With the box ticked, only a cell that says bike and nothing else changes, and the cell biked stays biked.
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?
40 + 35 + 45 + 50 + 45 + 40 = 255, and 255 ÷ 6 = 42.5 minutes. Before the fix the sum was 660, and 660 ÷ 6 = 110 minutes. One cell moved the mean from 110 to 42.5. The 450 could have been right, so it was an outlier. The planner is a record that already exists, so the student checked it, then fixed the cell and logged why.
A column holds 30, 60, a blank cell, 90 and 60. What does =AVERAGE show?
=AVERAGE skips the blank, so it adds four values: 30 + 60 + 90 + 60 = 240, and 240 ÷ 4 = 60. Type a 0 in the blank and it shows 240 ÷ 5 = 48. A blank means unknown. A 0 means you measured, and it was nothing.
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?
The last two values are in hours: 1.5 hours is 90 minutes, and 0.5 hours is 30 minutes. That is drift. Nothing is missing, because COUNTA matches the 5 rows. No row repeats. The counts pass a drift, because 1.5 looks like an ordinary number. Reading the column top to bottom catches it. On CLEAN, type 90 and 30, and log each one.
Last week you typed 12 for a night you slept 7 hours. Your watch app still shows 7. Where do you fix it?
Nothing already in RAW is ever changed. RAW is the only record of what you typed. Every fix happens on CLEAN and gets a row in the CLEANING LOG, with its Why and its How. The watch app is a record that already exists, so the 7 is allowed. Why: the watch app shows 7 hours for that night. How: Typed in the cell.
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.