Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

6.8 (4 of 4): Data wrangling in five steps

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

The messy sign-up sheet

Your class signs up for a trip in the group chat. Three people type their details. Look:

" sam patel ,16, ls1 1ab , £4.50 " "AISHA KHAN,160,m60 1nw,£3.00" "leo brown,14,ls2 9xy,£2"

The capital letters are all over the place. There are extra spaces. One age is 160. You cannot send this to the bus company. First you must tidy it.

Key words

WordWhat it meansExample
Data wranglingTurning messy raw data into tidy data.Fixing the sign-up sheet
StructureTo put data into rows and fields.Split a line at each comma.
CleanTo fix or remove small mistakes."sam patel" becomes "SAM PATEL"
ValidateTo check that each value is sensible.Age 160 is refused.
EnrichTo add useful new facts to the data.Add a ticket type.
OutputTo save or send the tidy data.A CSV file
FieldOne piece of information in a record.age
CSVA text file where each line is a row and commas split the fields.Sam,16,LS1 1AB

The cells students fill in are left blank in this preview.

What is data wrangling, and why?

Data wrangling turns messy raw data into tidy data. Raw data from people, forms, machines and other systems is rarely ready to use.

Wrangle data before you analyse it or put it in a store. Bad data in gives bad answers out. Use it when the data comes from many places or many people.

Quick check: what is wrong?

Have a go

Quick check: what is wrong?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Before and after

Raw dataProblemTidy data
sam patel ,16, ls1 1ab Spaces and small lettersSAM PATEL,16,LS1 1AB
AISHA KHAN,160,m60 1nwAge is not sensibleNot kept
leo brown,14,ls2 9xySmall lettersLEO BROWN,14,LS2 9XY

The cells students fill in are left blank in this preview.

Wrangling has five steps

A flow from "Raw data" through "1 Structure", "2 Clean", "3 Validate", "4 Enrich" and "5 Output" to "Ready to use", each with an arrow to the next.A flow from "Raw data" through "1 Structure", "2 Clean", "3 Validate", "4 Enrich" and "5 Output" to "Ready to use", each with an arrow to the next.

Steps 1 and 2: structure and clean

Structure: give the data a shape. Split each line at the commas, so every value has its own place.

Clean: fix small problems. Take off extra spaces. Write names in capital letters. Turn "£4.50" into the number 4.5.

Try it: structure and clean

Code: python
line = "  sam patel ,16, ls1 1ab , £4.50 "

fields = []
for part in line.split(","):
    fields.append(part.strip())
print("Structured:", fields)

name = fields[0].upper()
postcode = fields[2].upper()
price = float(fields[3].replace("£", ""))
print("Cleaned:", name, postcode, price)

# Change the line. Add a new person and press Run again.

Make a free account to save your work and track your progress.

Steps 3, 4 and 5: validate, enrich, output

Validate: check each value is sensible. Age 160 goes.

Enrich: add something useful. A ticket type, a town from the postcode, or the day of the week from a date.

Output: save the tidy data in the shape the next step needs, such as a CSV file.

Put the steps in order

Have a go

Put the steps in order

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Match the job to the step

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Your turn: build the five steps

Build the five steps of data wrangling for the trip sign-up sheet. The last function, wrangle, is already finished and uses your five. Read the comment in each function. Press Run Tests to check.

Code: python
def structure(line):
    # Step 1. Split the line on commas. Take the spaces off each part.
    # "  sam patel ,16, ls1 1ab , £4.50 " becomes ["sam patel", "16", "ls1 1ab", "£4.50"]
    return []


def clean(fields):
    # Step 2. Tidy the four fields and give back a dictionary:
    # name: all capital letters ("sam patel" becomes "SAM PATEL")
    # age: a whole number
    # postcode: all capital letters
    # price: a number with a decimal point, with the £ sign taken off
    return {}


def validate(record):
    # Step 3. Give back True only if ALL are true:
    # age is from 11 to 19, the postcode has a space in it, the price is 0 or more.
    return None


def enrich(record):
    # Step 4. Give back a copy of the record with one new key, "ticket":
    # "junior" if the age is under 16, otherwise "standard".
    return {}


def output(records):
    # Step 5. Give back text. The first line is: name,age,postcode,ticket
    # Then one line for each record, with the values in that order, joined by commas.
    # Join the lines with a new line mark. Do not put a new line mark at the end.
    return ""


def wrangle(lines):
    # This function is finished. It uses your five steps.
    good = []
    for line in lines:
        record = clean(structure(line))
        if validate(record):
            good.append(enrich(record))
    return output(good)

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Watch: the five steps

Press Play on the short film below. It takes the messy sign-up sheet through all five steps. Listen for the myth about clean and validate.

Film: Data wrangling in five steps

Data wrangling in five steps

Every scene, as text

  1. The messy sign-up sheet. " sam patel ,16, ls1 1ab " (problem: spaces and small letters). "AISHA KHAN,160,m60 1nw" (problem: age is not sensible). "leo brown,14,ls2 9xy" (problem: small letters). You cannot send this to the bus company.
  2. What is data wrangling, and why? Messy raw data, an arrow to tidy data (data wrangling: from messy to tidy). Wrangle before you analyse it or put it in a store (why: bad data in gives bad answers out). Raw data is rarely ready to use (when: data from many places or people).
  3. Wrangling has five steps. Raw data, an arrow down to 1 Structure, then 2 Clean, 3 Validate, 4 Enrich and 5 Output, each with an arrow down to the next, and an arrow down to Ready to use.
  4. Steps 1 and 2: structure and clean. " sam patel ,16, ls1 1ab ". An arrow down to three fields, one a line, their spaces still on them: " sam patel ", "16" and " ls1 1ab " (structure: every value has its own place). An arrow down to "SAM PATEL", 16 and "LS1 1AB" (clean: no extra spaces, capital letters).
  5. Steps 3, 4 and 5: validate, enrich, output. "AISHA KHAN,160,M60 1NW", struck through (validate: not sensible, so not kept). A CSV file: "name,age,postcode,ticket" (output: a CSV file for the bus company), "SAM PATEL,16,LS1 1AB,standard" (enrich: standard ticket, not under 16), "LEO BROWN,14,LS2 9XY,junior" (enrich: junior ticket, under 16).
  6. Myth (false): clean and validate are the same. That is not true: clean fixes small mistakes, validate checks each value is sensible. "sam patel" becomes "SAM PATEL" (clean: fixes a small mistake). Age 160 is refused (validate: checks the value is sensible). Clean first, then validate (why: only a clean number can be checked).

Final check

Have a go

Final check

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

Tip

Wrangling has five steps: structure, clean, validate, enrich, output. Do them in that order, before you analyse or store the data.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.