Understand the idea
to_numeric parses numeric text. Choose what happens when a string cannot be interpreted as a number.
A small example
["8.5", "bad", missing] → errors="coerce" → [8.5, missing, missing]. Only "bad" caused a new gap.
Follow the code
Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.
df["price"] = pd.to_numeric(df["price"], errors="coerce")
dfWhat each part does
df["price"] =- Replace the text column with the parsed numeric values.
pd.to_numeric(df["price"], errors="coerce")- Try to turn each price string into a number.
errors="coerce"- Make invalid text missing (NaN) instead of stopping with an error.
Your inputs
The editable setup on the right creates df. Run executes the setup and your work from top to bottom.
| order | drink | size | price | tip | date |
|---|---|---|---|---|---|
| 101 | " latte " | LARGE | 6.20 | 1 | 2026-06-01 |
| 102 | TEA | small | 3.10 | 0.5 | 2026-06-02 |
| 103 | " mocha " | LARGE | oops | None | not a date |
| 104 | Latte | Small | None | 0.8 | 2026-06-04 |
| 104 | Latte | Small | None | 0.8 | 2026-06-04 |
| 105 | "tea " | SMALL | 4.20 | 0.6 | 2026-06-05 |
| 106 | ESPRESSO | small | 2.50 | 0.2 | 2026-06-06 |
| 107 | " mocha" | large | 6.80 | 1.5 | 2026-06-07 |
Your task · Follow
- Convert price to numeric in df, making invalid text missing.
- Preserve every row.
- Keep the changes in df and display it.
- Use: to_numeric().
Hint
errors="coerce" creates missing values for invalid strings; it does not repair the original text.
Reveal solution
One way to do it. Keep any supplied setup in the editor and use this in the Your work section.
df["price"] = pd.to_numeric(df["price"], errors="coerce")
df