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 - You can use a lambda function to apply Python’s built-in float() function to each element in the Series. This method provides the flexibility of Python functions with pandas’ Series apply method for cases that may require additional logic within the conversion process.
Discussions

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
Convert Series to float data in python pandas - Stack Overflow
I have written code to find maximum value horizontally. Now I want to store it to other location in data frame using .iloc[] function. I am not able to do it as dtype of it is series. How to convert More on stackoverflow.com
🌐 stackoverflow.com
December 22, 2017
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
How do I convert a object to a float.
Can you post some sample data here? It sounds like one of your columns may contain strings of digits. In programming in general "2" and 2 are different. "2" != 2, but int("2") == 2. More on reddit.com
🌐 r/learnpython
3
2
April 7, 2022
🌐
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.Series.convert_dtypes.html
pandas.Series.convert_dtypes — pandas 3.0.6 documentation
By default, convert_dtypes will attempt to convert a Series (or each Series in a DataFrame) to dtypes that support pd.NA. By using the options convert_string, convert_integer, convert_boolean and convert_floating, it is possible to turn off individual conversions to StringDtype, the integer extension types, BooleanDtype or floating extension types, respectively.
🌐
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?
🌐
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 › 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 ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_numeric.html
pandas.to_numeric — pandas 3.0.6 documentation - PyData |
Due to the internal limitations ... (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....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-pandas-series-astype-to-convert-data-type-of-series
Python | Pandas Series.astype() to convert Data type of series - GeeksforGeeks
October 1, 2018 - Syntax: DataFrame.astype(dtype, copy=True, errors='raise') Parameters: dtype: Data type to convert the series into. (for example str, float, int) copy: Makes a copy of dataframe/series. errors: Error raising on conversion to invalid data type. For example dict to string.
🌐
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
May 1, 2026 - 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.
🌐
Stack Overflow
stackoverflow.com › questions › 47942270 › convert-series-to-float-data-in-python-pandas
Convert Series to float data in python pandas - Stack Overflow
December 22, 2017 - If you don't want b converted to float, don't use downcast='integer'. ... That's incorrect. The slice's size must be the same as b. ... By finding maximum value using a = master.iloc[:, 154:181].max(axis=1), gives series dtype.
🌐
GoLinuxCloud
golinuxcloud.com › home › databases › pandas › 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 - The most straightforward way to convert a pandas column to float is using the astype() method. This method explicitly casts the column data type to float.
🌐
Stack Overflow
stackoverflow.com › questions › 68172978 › pandas-core-series-series-to-float
python - pandas.core.series.Series to float - Stack Overflow
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.
🌐
Reddit
reddit.com › r/learnpython › how do i convert a object to a float.
r/learnpython on Reddit: How do I convert a object to a float.
April 7, 2022 -

Hi guys, im struggeling with my dataframe.

Im trying to get a percentage of 2 columns, but when I divide the 2 I get this error: unsupported operand type(s) for /: 'float' and 'str'

The dtypes of the columns are an 'object'

Im aware that I have empty data in my columns, but is that an issue?

Would appreciate your help!

🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Convert between DataFrame and Series | note.nkmk.me
January 20, 2024 - pandas: How to use astype() to cast dtype of DataFrame · Be careful when retrieving a row of a DataFrame as a Series. For example, retrieving a row from a DataFrame that has columns of integer (int) and floating-point number (float) types as a Series results in a float data type, with the values in the int column converted to float.