Unit 5: Data analysis · Data tools

Article

Cleaning messy data

7 min read

A school asks its students, "Which city do you live in?" The answers include Delhi, delhi and Dehli. A program that counts the answers sees three different cities, though they are all one. The program is correct. The data is the problem. Fixing the data before you use it is called cleaning.

What goes wrong in real data

Real data comes from people, sensors and forms, so it is rarely perfect. Here is a survey table with six kinds of trouble:

idcityageheight
1Delhi15160
2delhi161.72
3Dehli15158
4Pune165
5Pune14150
5Pune14150
6Goa215170
ProblemWhere it isHow to fix it
Inconsistent formatdelhi and Delhi. 1.72 is metres, but the others are cmchoose one form and change every value to it: Delhi, and 172
TypoDehlicorrect it to Delhi
Missing valuerecord 4 has no agemark it clearly as missing, or find the real value
Duplicaterecord 5 appears twiceremove the extra copy
Invalid valuean age of 215 cannot be truemark it as missing, or correct it if you know the truth

Cleaning means making the data uniform without changing its meaning. Changing delhi to Delhi and 1.72 m to 172 cm keeps the meaning, so it is good cleaning. Changing 215 to 15 because "it was probably a typo" invents a fact, so do not do it unless you can check.

Check your understanding

Which action is data cleaning that makes the data uniform without changing its meaning?

A program can be right and the answer wrong

Count the students from Delhi, first on the raw city list, then on the cleaned one:

AP pseudocode
count ← 0
FOR EACH city IN cities
{
    IF(city = "Delhi")
    {
        count ← count + 1
    }
}
DISPLAY(count)

With the raw list ["Delhi", "delhi", "Dehli", "Pune"] it displays 1. Only the first value matches exactly. Three students live in Delhi. The code has no bug: it did what it was told, on messy data. Run the Python version, which cleans first:

Runs in your browser

The cleaning loop is a transform from the last article: it makes a new, uniform list. Keep a copy of the raw data too, so you can always check what you changed. Cleaning can also be a problem when data from different sources is combined, because each source may use its own format.

Clean is not the same as fair

Data can be perfectly tidy and still mislead you.

  • Incomplete data has parts missing. If one class never answered a survey, the results say nothing about that class.
  • Biased data does not fairly represent the group you want to know about. This is collection bias. A poll about exercise, held outside a gym, asks mostly people who like exercise. An online survey misses people with no Internet.
  • More data is not automatically better. A million answers from a biased source are a million biased answers. A big data set also costs more to store and process.

Cleaning cannot repair these problems, because the missing or unfair part was never collected. Before you trust a result, ask: who is in this data, and who is not?

Check your understanding

A school asks students how they travel to school, but hands out the survey only on the school buses. Why may the results be unreliable?

Common mistakes

  • Blaming the code. If the program is correct, look at the data.
  • Deleting every record that has a problem. You may lose good information, and you may lose it unevenly.
  • Guessing a fix. Replacing an invalid value with a guess changes the meaning.
  • Thinking cleaning makes data fair. Cleaning fixes the format. It cannot fix who was left out.
  • Thinking more data fixes bias. A bigger biased sample is still biased.
  • Cleaning the only copy. Keep the raw data.

On the exam

You may get a table with messy values and be asked which step makes it ready (make the spelling consistent, remove duplicates, convert units). Another type describes a survey and asks why the results may be unreliable. Look for who was not asked. A third type asks which change would not alter the meaning of the data.

Key words

Cleaning data: making data uniform and usable without changing its meaning.

Missing value: a field with nothing in it.

Duplicate: a record that appears more than once.

Invalid value: a value that cannot be true, such as an age of 215.

Incomplete data: data with parts missing.

Collection bias: data that was collected in a way that unfairly favours some of the group.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.