We use to_numeric() to change data into numbers safely. It helps avoid errors when some data can't be turned into numbers.
to_numeric() for safe conversion in Pandas
pandas.to_numeric(arg, errors='raise', downcast=None)
arg is the data to convert (like a list or a DataFrame column).
errors controls what happens if conversion fails: 'raise' (stop), 'coerce' (make bad data NaN), or 'ignore' (keep original).
pd.to_numeric(['1', '2', '3'])
pd.to_numeric(['1', 'two', '3'], errors='coerce')
pd.to_numeric(['1', 'two', '3'], errors='ignore')
This code shows how to convert a list of strings to numbers safely. The words 'thirty' and 'fifty' cannot be converted, so they become NaN (missing values) when using errors='coerce'.
import pandas as pd # Sample data with some non-numeric values data = ['10', '20', 'thirty', '40', 'fifty'] # Convert with errors='raise' (default) - will cause error if uncommented # pd.to_numeric(data) # Convert with errors='coerce' to safely convert and replace bad data with NaN converted = pd.to_numeric(data, errors='coerce') print(converted)
Using errors='coerce' is helpful to avoid program crashes when data has unexpected text.
Result will be floats if there are any NaN values, because NaN is a float type.
You can use downcast to reduce memory, for example to 'integer' or 'float'.
to_numeric() safely converts data to numbers.
Use errors='coerce' to handle bad data by turning it into NaN.
This helps clean data before analysis without stopping your code.