Turning Text into Numbers: to_numeric
Text such as '59 lbs' and '$75' becomes numbers in two steps, .str.replace to take off the units and symbols, then pd.to_numeric with errors='coerce' to convert what's left, followed by a list of the values that became missing so none is lost silently.
Apprentice · Python · Updated
After this lesson you can
- Take symbols and units such as '
#39;, ',' and ' lbs' off a text column with .str.replace - Convert text to numbers with pd.to_numeric(errors='coerce'), and list the values that became missing
Key takeaways
- .str.replace() changes text in every value of a column at once, such as taking ' lbs' off '59 lbs' or '