to_numeric()
noun
say it: “too noo-MAIR-ik” /tu nuˈmɛrɪk/
A pandas function that turns text such as "12.5" into numbers, and can mark each value it can't read as missing instead of stopping.
What does to_numeric() do in pandas?
to_numeric() (pd.to_numeric) turns a column of text, such as "59" and "12.5", into numbers. On its own it
stops at the first value it can't read; with errors="coerce", each such value becomes NaN instead, so you can count
and look at them afterward.
import pandas as pd
weights = pd.Series(["59", "12.5", "about 40"])
print(pd.to_numeric(weights, errors="coerce"))
# 0 59.0
# 1 12.5
# 2 NaN
In a sentence
- “The weights were text until to_numeric() made them numbers she could average.”
- “to_numeric() with errors="coerce" turned the one unreadable weight into NaN.”
Level: Journeyman · More Python terms at this level
Updated