Data Science · Level 3 Cleaning

Fixing Column Types in pandas

Turn a price column like "$1,299.00" into real numbers with .str.replace() and pd.to_numeric(), so totals and averages work.

  • dtype object means text
  • .str.replace() to strip symbols
  • pd.to_numeric(errors="coerce")

Exported prices like "$1,299.00" are text (dtype object). Trap: sum() then glues the strings together instead of adding. Strip the symbols and convert with pd.to_numeric; errors="coerce" turns junk into NaN.

s = pd.Series(["$5.00", "$1,200.50", "n/a"])
clean = s.str.replace("$", "", regex=False).str.replace(",", "", regex=False)
pd.to_numeric(clean, errors="coerce").tolist()   # [5.0, 1200.5, nan]

Write parse_prices(s) that converts a Series of prices like "$1,200.50" into floats, with NaN for anything that isn't a price, for example ["$3", "free"] becomes [3.0, nan].

solution.py
Test Suites Pending
Output