pandas: to_numeric() for Safe Conversion of Strings to Numbers in Pandas#

pandas.to_numeric(arg, errors='raise', downcast=None, dtype_backend=<no_default>)

In pandas, pandas.to_numeric() allows you to convert a column or Series of strings to numeric values (integers or floats).

Basic usage:

>>> import pandas as pd
>>> s = pd.Series(['1.0', '2', -3])
>>> pd.to_numeric(s)
0   1.0
1   2.0
2   -3.0
dtype: float64
>>> pd.to_numeric(s, downcast='float')
0   1.0
1   2.0
2   -3.0
dtype: float32
>>> pd.to_numeric(s, downcast='integer')
0    1
1    2
2   -3
dtype: int8
>>> pd.to_numeric(s, downcast='signed')
0    1
1    2
2   -3
dtype: int8
>>> pd.to_numeric(s, downcast='unsigned')
0    1
1    2
2    3
dtype: uint8
>>> s = pd.Series(['apple', '1.0', '2', -3])
>>> pd.to_numeric(s, errors='coerce')
0    NaN
1    1.0
2    2.0
3   -3.0
dtype: float64
>>> pd.to_numeric(s, errors='coerce').fillna(0)
0    0.0
1    1.0
2    2.0
3   -3.0
dtype: float64

For more information about nullable types, refer to the following article:

Downcast to nullable (can be marked as NaN/) integer or float is supported:

>>> s = pd.Series([1, 2, 3, None], dtype="Int64")
>>> pd.to_numeric(s, downcast="integer")
0       1
1       2
2       3
3    <NA>
dtype: Int8
>>> s = pd.Series([1.1, 2.2, 3.3, None], dtype="Float64")
>>> pd.to_numeric(s, downcast="float")
0       1.1
1       2.2
2       3.3
3    <NA>
dtype: Float32