🎓 A1Learner
🔍
0 of 52 lessons completed

Python Project: A Data Cleaning Pipeline

A pipeline is a row of small steps that clean data. Each step does one job, and the data flows through them in order.

Step one: strip the spaces

map() with str.strip cleans every name. The spaces at the edges go away.

raw = ["  ada ", "BOB", "  cy  ", ""]
print(list(map(str.strip, raw)))

Step two: drop the blanks

filter() keeps only the names that are not empty.

names = ["ada", "BOB", "cy", ""]
print(list(filter(lambda s: s != "", names)))

Step three: sort by length

sorted() with key=len orders the clean names by length.

names = ["ada", "BOB", "cy"]
print(sorted(names, key=len))

Example

raw = ["  ada lovelace ", "BOB ", "  cy", "", "  dee hopper"]

step1 = list(map(str.strip, raw))
step2 = list(filter(lambda s: s != "", step1))
step3 = list(map(str.title, step2))
step4 = sorted(step3, key=len)

for place, name in enumerate(step4, start=1):
    print(place, name)

Key points

  • Pipeline: a row of small steps, each doing one job on the data
  • Use small functions for the steps, so each step can be tested alone
  • map() and filter() do the steps. sorted() with a key orders the result.
  • Steps run in written order. Each step sees the last step's result.
  • A pipeline is easy to fix, because a wrong result points at one step.

In this example, the raw list holds messy names with spaces, mixed case, and one blank. Step one strips the spaces. Step two drops the blank. Step three title-cases each name. Step four sorts by length. The numbered printout is the clean list, shortest name first, and each step's result feeds the step after it.

Note

This project uses only the tools from this entry: map(), filter(), and sorting with a key.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which step runs first in this pipeline?