Find a course

Module 3: Fixing Values and TypesLesson 5 of 1214 min

Numbers Stored as Text and Other Type Problems

Parse prices with currency symbols, commas and accounting negatives, handle decimal commas and mixed units, and avoid silent conversion failures.

Step 1 of 11Learn

The revenue total that was 18% too low

A finance analyst summed the amount column of an invoice export in a spreadsheet and reported monthly revenue of $412,000. The real figure was about $503,000. No formula was wrong. Roughly one invoice in six had been exported with the amount as text, like "$1,250.00", and the spreadsheet's SUM function quietly skips text cells.

This is one of the most common and costly cleaning problems: numbers stored as text. To a computer, the string "1,250" is just five characters. It cannot be added, averaged or compared by size. Sorting text numbers puts "100" before "25", because the comparison goes character by character and "1" comes before "2".

Tools react differently. Some skip text silently, like that SUM. Some concatenate: in Python, "12" + "8" gives "128". Some raise an error, which is actually the friendliest outcome, because at least you notice.

The fix is to parse text into a proper numeric type with explicit rules, then check that every value converted. In this lesson you will learn the usual reasons numbers end up as text, how to strip them back to digits safely, and how to catch the values that refuse to convert instead of losing them.

Revenue reported versus actual when text amounts are skipped by SUMSUM of the column$412kActual revenue$503kAbout one invoice in six was stored as text

Step 2 of 11Learn

Why numbers turn into text

A column loads as text if even one cell can't be read as a number. The usual culprits:

  • Currency symbols and codes: "$49.90", "EUR 12", "£7".
  • Thousands separators: "1,250,000".
  • Units glued to values: "12 kg", "350ml", "4.5%".
  • Accounting negatives: "(320.00)" means minus 320 in many financial reports.
  • Stray characters: leading or trailing spaces, footnote marks like "1,200*", non-breaking spaces copied from web pages.
  • Placeholders: "N/A" or "-" in a single cell turns the whole column into text.
  • Regional formats: in much of Europe and Latin America, "1.234,56" means one thousand two hundred thirty-four point five six.

The last one is the most dangerous, because a wrong guess does not fail; it produces a different valid number. Read with US conventions, "1.234" becomes one point two three four, a value 1,000 times too small, and nothing complains.

So parsing starts with a decision per column: which symbols to remove, which character is the decimal separator, and what a negative looks like. Write those rules down, apply them in code, and then count how many values still fail to convert.

Seven reasons numbers load as textNumber as textCurrency1,000scommasUnits(Negat-ives)StraycharsN/ARegionalformat

Step 3 of 11Practice · Choose one

A product file from a German supplier has a price column with values such as "1.299,00" and "12,50". Parsed with US conventions, what goes wrong?

Step 4 of 11Practice · Code challenge · Python

Write parse_price(text) that converts a price string to a float. Remove surrounding spaces, the symbols $, € and £, and commas used as thousands separators. Treat a value wrapped in parentheses, like "(30.50)", as negative. Return None for an empty or space-only string.

Real Python, running in a sandbox in your browser.
  • parse_price("$1,299.00")
  • parse_price(" (30.50) ")
  • parse_price("€45")
  • parse_price(" ")
  • parse_price("12.5 ")

Step 5 of 11Learn

Coercion: converting without losing track

When you convert a whole column, some values will refuse: "call for price", "TBD", "12 to 15". Most tools offer a coerce option that turns anything unparseable into a missing value instead of stopping. In pandas it's pd.to_numeric(col, errors="coerce"); in SQL, functions like TRY_CAST do the same.

Coercion is convenient and dangerous. If a supplier changes its export so that every price now carries a currency code, coercion will happily turn 100% of the column into missing values, and your next average will be calculated from nothing, or from the few rows that happened to be clean.

The safe habit has three parts:

  1. Count missing values before and after conversion. If 12 values were blank before and 340 are blank after, 328 values failed to parse.
  2. Look at the failures. Print the distinct original strings that became missing. Often a single pattern, such as "USD 45.00", explains most of them and can be handled with one more rule.
  3. Decide explicitly. Some failures are true unknowns ("TBD") and should be missing. Others contain real numbers in a messy form and deserve a parsing rule.

Treat a conversion step as finished only when you can say how many values failed and why.

Safe coercion: count missing values before and after convertingCount missingbeforeCoerceto numberCount missingafterInvestigateany jump

Step 6 of 11Practice · Sort into groups

Sort these raw values: can a simple rule (strip symbols, spaces and thousands commas) convert them correctly, or do they need a decision first?

Tap an item, then tap its group (or drag it there).

Step 7 of 11Note

Step 8 of 11Learn

Booleans, units and consistent scales

Type problems are not limited to numbers. Yes/no fields arrive as "Y", "yes", "TRUE", "1", "x", "checked" or blank. Decide on one representation, usually True and False or 1 and 0, map every variant to it, and make sure blank is treated as unknown rather than quietly becoming False.

Mixed units are subtler because every value is a valid number. A patient weight column might hold kilograms from one clinic and pounds from another. A shipping table might mix grams and kilograms. Profiling helps: a bimodal histogram, or a cluster of values about 2.2 times larger than the rest, is a strong hint.

The fix is to convert everything to one unit, using a column or a source field that says which unit each row used, and to put the unit in the column name: weight_kg, price_usd, duration_min. Then nobody has to guess.

Scale problems belong here too. Percentages may be stored as 45 in one system and 0.45 in another. Money may be in dollars in one table and cents in another, as in the retailer story at the start of the course. Before combining sources, check that the same column means the same thing, on the same scale, everywhere.

Step 9 of 11Practice · Work it out

A clinic recorded a patient's weight as 154 lb. Your cleaned column is weight_kg. Using 1 lb = 0.4536 kg, what value should you store? Round to one decimal place.

Step 10 of 11Practice · True or false

Using a "coerce" option to turn unparseable values into missing values is a safe default, because those values were probably garbage anyway.

Step 11 of 11Practice · Choose one

An exported column of product weights sorts like this: "100", "1200", "25", "300". What is the most likely explanation?