Getting and cleaning data · lesson 8 of 11
Parts in this lesson
Anp.where(unit == "g", qty / 1000, qty)2.7: one unit for every row
Bdifflib.get_close_matches(name, names, cutoff=0.85)names that are nearly the same
Cmerge(..., validate="many_to_one")4.3: a join that stops on a doubtful key

Units, codes and consistency

Last updated

Two values can only be compared if they mean the same thing: the same unit, the same spelling, the same place. In this lesson you'll convert grams and centavos, match place names to a list of codes, use fuzzy matching carefully, and see why a town called San Jose can double your orders.

By the end of this lesson you can

  • Convert mixed units into one, using a unit column
  • Match names to a list of codes, exactly first
  • Use fuzzy matching with a cut-off, and send doubtful matches to a person
  • Explain why names aren’t keys, and codes are

Units

Four deliveries: two weighed in kilos, two in grams, and prices in centavos, as the supplier's system keeps them. Run the cells.

units.ipynb

Adding up qty gives 2,075, which means nothing: 25 kg plus 500 g isn't 525 of anything. np.where converts the grams (lesson 2.7) using the unit column, so every row is kilos: 77 kg in all. Prices in centavos are divided by 100.

Rules for units:

  • Convert to one unit per column as early as possible, and put the unit in the column's name: kilos, pesos, rain_mm.
  • Convert using a unit column, never by guessing from the size of the number. "Anything over 500 must be grams" goes wrong the day someone buys 600 kg.
  • Money is often stored in centavos as whole numbers, because whole numbers add up exactly and decimals don't always (lesson 2.3). Divide only for showing.

Codes and lookups

Names are for people; codes are for joining. In the Philippines, the PSA gives every region, province, city, municipality and barangay a Philippine Standard Geographic Code (PSGC). Two datasets that both have PSGC codes join exactly, however each one spells the town.

The course's towns.csv is a small lookup table in the same spirit, with course-made codes (T01, T02…), not real PSGC codes. Step through the matching, then run it.

Matching typed names to codes. Six town names as people typed them, to be matched to the course’s list of towns and codes.Lipa Citylipa city Sto. TomasTanuan CitySan JoseLipatyped name → town codetypedcodehow
1 of 4 Six town names as people typed them, to be matched to the course’s list of towns and codes.
match.ipynb

The match function tries the safe things first:

  1. Tidy: strip, title and Sto. → Santo (lesson 4.8). Then an exact match.
  2. If more than one town has that name, ask a person. Don't pick.
  3. Otherwise try a close match: difflib.get_close_matches scores how alike two strings are, from 0 to 1, and returns those above the cut-off, 0.85 here. Tanuan City scores well above it against Tanauan City, and nothing else does.
  4. Anything else, ask a person. Lipa is too short to be close to Lipa City; you'd guess Lipa City, but a guess isn't a match.

Keep the how column: anyone checking the result can see which codes were exact and which were judgement calls. Then add each answer you get from a person to a list of known spellings, so it's never asked twice.

Fuzzy matching is a way to make a short list for a person to check, not a way to skip the checking. Libraries like rapidfuzz are faster and have more scores; the care needed is the same.

Wrong way: one San Jose

A supplier's orders name the town only: San Jose. Run the cells.

sanjose.ipynb

get_close_matches happily returns San Jose: a perfect score. But there's a San Jose in Batangas and another in Occidental Mindoro (and San Jose City in Nueva Ecija, and more around the country). Joining on the name matches the order to both towns: 2 orders become 3 rows, and 550 kg instead of 350.

A town name isn't a key; the town and province together are closer, and a code is best. The last cell is the safety net from lesson 4.3: validate="many_to_one" stops with a MergeError because a town name appears more than once in the lookup table, so the join can't be trusted. Put it on every lookup join.

Check what you learned

A column has kilos, but some stores send grams. How should you convert?
get_close_matches finds one town above the cut-off. What next?
What stops a lookup join from quietly doubling rows?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis