Use pd.to_numeric with errors='coerce'

# Setup
s = pd.Series(['1', '2', '3', '4', '.'])
s

0    1
1    2
2    3
3    4
4    .
dtype: object

pd.to_numeric(s, errors='coerce')

0    1.0
1    2.0
2    3.0
3    4.0
4    NaN
dtype: float64

If you need the NaNs filled in, use Series.fillna.

pd.to_numeric(s, errors='coerce').fillna(0, downcast='infer')

0    1
1    2
2    3
3    4
4    0
dtype: float64

Note, downcast='infer' will attempt to downcast floats to integers where possible. Remove the argument if you don't want that.

From v0.24+, pandas introduces a Nullable Integer type, which allows integers to coexist with NaNs. If you have integers in your column, you can use

pd.__version__
# '0.24.1'

pd.to_numeric(s, errors='coerce').astype('Int32')

0      1
1      2
2      3
3      4
4    NaN
dtype: Int32

There are other options to choose from as well, read the docs for more.


Extension for DataFrames

If you need to extend this to DataFrames, you will need to apply it to each row. You can do this using DataFrame.apply.

# Setup.
np.random.seed(0)
df = pd.DataFrame({
    'A' : np.random.choice(10, 5), 
    'C' : np.random.choice(10, 5), 
    'B' : ['1', '###', '...', 50, '234'], 
    'D' : ['23', '1', '...', '268', '$$']}
)[list('ABCD')]
df

   A    B  C    D
0  5    1  9   23
1  0  ###  3    1
2  3  ...  5  ...
3  3   50  2  268
4  7  234  4   $$

df.dtypes

A     int64
B    object
C     int64
D    object
dtype: object

df2 = df.apply(pd.to_numeric, errors='coerce')
df2

   A      B  C      D
0  5    1.0  9   23.0
1  0    NaN  3    1.0
2  3    NaN  5    NaN
3  3   50.0  2  268.0
4  7  234.0  4    NaN

df2.dtypes

A      int64
B    float64
C      int64
D    float64
dtype: object

You can also do this with DataFrame.transform; although my tests indicate this is marginally slower:

df.transform(pd.to_numeric, errors='coerce')

   A      B  C      D
0  5    1.0  9   23.0
1  0    NaN  3    1.0
2  3    NaN  5    NaN
3  3   50.0  2  268.0
4  7  234.0  4    NaN

If you have many columns (numeric; non-numeric), you can make this a little more performant by applying pd.to_numeric on the non-numeric columns only.

df.dtypes.eq(object)

A    False
B     True
C    False
D     True
dtype: bool

cols = df.columns[df.dtypes.eq(object)]
# Actually, `cols` can be any list of columns you need to convert.
cols
# Index(['B', 'D'], dtype='object')

df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')
# Alternatively,
# for c in cols:
#     df[c] = pd.to_numeric(df[c], errors='coerce')

df

   A      B  C      D
0  5    1.0  9   23.0
1  0    NaN  3    1.0
2  3    NaN  5    NaN
3  3   50.0  2  268.0
4  7  234.0  4    NaN

Applying pd.to_numeric along the columns (i.e., axis=0, the default) should be slightly faster for long DataFrames.

Answer from coldspeed95 on Stack Overflow
Top answer
1 of 3
94

Use pd.to_numeric with errors='coerce'

# Setup
s = pd.Series(['1', '2', '3', '4', '.'])
s

0    1
1    2
2    3
3    4
4    .
dtype: object

pd.to_numeric(s, errors='coerce')

0    1.0
1    2.0
2    3.0
3    4.0
4    NaN
dtype: float64

If you need the NaNs filled in, use Series.fillna.

pd.to_numeric(s, errors='coerce').fillna(0, downcast='infer')

0    1
1    2
2    3
3    4
4    0
dtype: float64

Note, downcast='infer' will attempt to downcast floats to integers where possible. Remove the argument if you don't want that.

From v0.24+, pandas introduces a Nullable Integer type, which allows integers to coexist with NaNs. If you have integers in your column, you can use

pd.__version__
# '0.24.1'

pd.to_numeric(s, errors='coerce').astype('Int32')

0      1
1      2
2      3
3      4
4    NaN
dtype: Int32

There are other options to choose from as well, read the docs for more.


Extension for DataFrames

If you need to extend this to DataFrames, you will need to apply it to each row. You can do this using DataFrame.apply.

# Setup.
np.random.seed(0)
df = pd.DataFrame({
    'A' : np.random.choice(10, 5), 
    'C' : np.random.choice(10, 5), 
    'B' : ['1', '###', '...', 50, '234'], 
    'D' : ['23', '1', '...', '268', '$$']}
)[list('ABCD')]
df

   A    B  C    D
0  5    1  9   23
1  0  ###  3    1
2  3  ...  5  ...
3  3   50  2  268
4  7  234  4   $$

df.dtypes

A     int64
B    object
C     int64
D    object
dtype: object

df2 = df.apply(pd.to_numeric, errors='coerce')
df2

   A      B  C      D
0  5    1.0  9   23.0
1  0    NaN  3    1.0
2  3    NaN  5    NaN
3  3   50.0  2  268.0
4  7  234.0  4    NaN

df2.dtypes

A      int64
B    float64
C      int64
D    float64
dtype: object

You can also do this with DataFrame.transform; although my tests indicate this is marginally slower:

df.transform(pd.to_numeric, errors='coerce')

   A      B  C      D
0  5    1.0  9   23.0
1  0    NaN  3    1.0
2  3    NaN  5    NaN
3  3   50.0  2  268.0
4  7  234.0  4    NaN

If you have many columns (numeric; non-numeric), you can make this a little more performant by applying pd.to_numeric on the non-numeric columns only.

df.dtypes.eq(object)

A    False
B     True
C    False
D     True
dtype: bool

cols = df.columns[df.dtypes.eq(object)]
# Actually, `cols` can be any list of columns you need to convert.
cols
# Index(['B', 'D'], dtype='object')

df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')
# Alternatively,
# for c in cols:
#     df[c] = pd.to_numeric(df[c], errors='coerce')

df

   A      B  C      D
0  5    1.0  9   23.0
1  0    NaN  3    1.0
2  3    NaN  5    NaN
3  3   50.0  2  268.0
4  7  234.0  4    NaN

Applying pd.to_numeric along the columns (i.e., axis=0, the default) should be slightly faster for long DataFrames.

2 of 3
20
In [30]: pd.Series([1,2,3,4,'.']).convert_objects(convert_numeric=True)
Out[30]: 
0     1
1     2
2     3
3     4
4   NaN
dtype: float64
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-pandas-series-to-float
5 Best Ways to Convert Pandas Series to Float – Be on the Right Side of Change
February 19, 2024 - This function forces a cast of the Series elements to the float data type, making it handy for quick conversions of numerical data that don’t require special handling of non-numeric values. ... This snippet shows the conversion of a pandas Series containing string representations of numbers to floats using astype(float).
Discussions

python - Pandas series to floats - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... import pandas as pd data = {"col1":["2%", "3%", "4%", "5%"],"col2":["N/A", "N/A", "4%", "5%"]} df = pd.DataFrame(data) ... df = df.replace({'%': '', 'N/A': np.NaN}, regex=True).astype(float) col1 ... More on stackoverflow.com
🌐 stackoverflow.com
February 23, 2020
TypeError: cannot convert the series to <type 'float'>
Maybe there is one value that can´t be converted. run a.dtypes to check your column´s types. If it says object, you have a mixed bag. It´s possible that there a string that wont allow the operation. Also, you can make a function to learn which value is giving you trouble. Something like this: def check_value(row): print(row['col2'], row['col3']) col1_value = round(100.0 * row['col2'] / row['col3'], 1) return col1_value a['col1'] = a.apply(check_value,axis=1) This will print the last value that was converted. More on reddit.com
🌐 r/learnpython
14
1
December 22, 2022
python - pandas.core.series.Series to float - Stack Overflow
I am new to python and been struggling with this simple code : So in the last line, I got the object type: pandas.core.series.Series But this type is causing an error when I use it in 2D arrays, ho... More on stackoverflow.com
🌐 stackoverflow.com
July 15, 2021
python - Converting strings to floats in a DataFrame - Stack Overflow
How to covert a DataFrame column containing strings and NaN values to floats. And there is another column whose values are strings and floats; how to convert this entire column to floats. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert column to float in dataframe
Pandas Convert Column to Float in DataFrame - Spark By {Examples}
October 14, 2024 - By using pandas DataFrame.astype() and pandas.to_numeric() methods you can convert a column from string/int type to float. In this article, I will explain
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_numeric.html
pandas.to_numeric — pandas 3.0.3 documentation - PyData |
Due to the internal limitations of ndarray, if numbers smaller than -9223372036854775808 (np.iinfo(np.int64).min) or larger than 18446744073709551615 (np.iinfo(np.uint64).max) are passed in, it is very likely they will be converted to float so that they can be stored in an ndarray. These warnings apply similarly to Series since it internally leverages ndarray.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.convert_dtypes.html
pandas.Series.convert_dtypes — pandas 3.0.3 documentation
Otherwise, convert to an appropriate floating extension type. In the future, as new dtypes are added that support pd.NA, the results of this method will change to support those new dtypes. ... >>> df = pd.DataFrame( ... { ... "a": pd.Series([1, 2, 3], dtype=np.dtype("int32")), ...
🌐
Statology
statology.org › home › how to convert object to float in pandas (with examples)
How to Convert Object to Float in Pandas (With Examples)
May 11, 2022 - Notice that the points column now has a data type of float64. The following code shows how to use the to_numeric() function to convert the points column in the DataFrame from an object to a float:
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-strings-to-floats-in-pandas-dataframe
How to Convert String to Float in Pandas DataFrame - GeeksforGeeks
January 18, 2024 - Syntax: Series.astype(dtype, copy=True, errors=’raise’) Parameters: This method will take following parameters: dtype: Data type to convert the series into. (for example str, float, int).c · 3 min read How to Convert Integers to Strings in Pandas DataFrame?
Find elsewhere
🌐
Runebook.dev
runebook.dev › en › docs › python › library › functions › float
A Friendly Guide to Python's float() and Avoiding ValueError
import pandas as pd # A Pandas Series (like a column in a spreadsheet) data_series = pd.Series(["10.5", "20.1", "30.9"]) # Convert the entire Series to float float_series = data_series.astype(float) print("\nPandas Series converted:") print(float_series) # Output: # 0 10.5 # 1 20.1 # 2 30.9 # dtype: float64
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › convert-pandas-dataframe-column-to-float
Convert Pandas Dataframe Column To Float - GeeksforGeeks
July 23, 2025 - As you observe the output the data type of the string column is changed from object to float after using to_numeric() function. We can handle Non-convertible values, Missing values, and NaN values by using errors='coerce' parameter in ...
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-a-column-in-pandas-dataframe-from-string-to-float
How to Convert a Column in Pandas DataFrame from String to Float | Saturn Cloud Blog
June 19, 2023 - Converting a column in a pandas DataFrame from a string to a float is a simple task that can be accomplished using the astype() method. In this blog post, we covered the steps required to convert a column from a string to a float.
🌐
Delft Stack
delftstack.com › home › howto › python pandas › pandas convert object to float
How to Convert Object to Float in Pandas | Delft Stack
February 2, 2024 - This tutorial has extensively covered converting an object-type column to float in Pandas, showcasing three distinct approaches: using the astype() method, employing the to_numeric() function, and harnessing the power of the apply() function ...
🌐
GoLinuxCloud
golinuxcloud.com › home › databases › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & Practical Examples) | GoLinuxCloud
March 9, 2026 - Learn how to convert pandas DataFrame columns to float using astype(), to_numeric(), and other practical methods. This tutorial explains how to convert string and object columns to float, handle invalid values, convert multiple columns, and safely process large datasets in pandas.
🌐
Stack Overflow
stackoverflow.com › questions › 68172978 › pandas-core-series-series-to-float
python - pandas.core.series.Series to float - Stack Overflow
July 15, 2021 - Because in the first instance you're subsetting by passing a string to the DataFrame with a column name and in the second you're subsetting using a lists of strings. So the second instance actually looks like this: Volatility = sec_returns[['GME']].std() * 250 ** 0.5 · This returns a pd.Series rather than a float.
🌐
Wellsr
wellsr.com › python › python-convert-pandas-dataframe-string-to-float-int
Python with Pandas: Convert String to Float and other Numeric Types - wellsr.com
November 9, 2018 - This tutorial will show you how to convert Pandas DataFrame strings into floats or ints. Converting Pandas string data to numeric types is required before performing numeric calculations.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-pandasseries-from-dtype-object-to-float-and-errors-to-nans
Saturn Cloud | Saturn Cloud | The Control Plane for GPU Clouds
June 19, 2023 - You keep your existing BMaaS relationship, and Saturn Cloud is the customer-facing surface on top of it.
🌐
Sentry
sentry.io › sentry answers › python › change a column type in a dataframe in python pandas
Change a column type in a DataFrame in Python Pandas | Sentry
If we want to convert a column to a sensible numeric data type (integer or float), we should use the to_numeric function. If we want Pandas to decide which data types to use for each column, we should use the convert_dtypes method.