NOTE: pd.convert_objects has now been deprecated. You should use pd.Series.astype(float) or pd.to_numeric as described in other answers.

This is available in 0.11. Forces conversion (or set's to nan) This will work even when astype will fail; its also series by series so it won't convert say a complete string column

In [10]: df = DataFrame(dict(A = Series(['1.0','1']), B = Series(['1.0','foo'])))

In [11]: df
Out[11]: 
     A    B
0  1.0  1.0
1    1  foo

In [12]: df.dtypes
Out[12]: 
A    object
B    object
dtype: object

In [13]: df.convert_objects(convert_numeric=True)
Out[13]: 
   A   B
0  1   1
1  1 NaN

In [14]: df.convert_objects(convert_numeric=True).dtypes
Out[14]: 
A    float64
B    float64
dtype: object
Answer from Jeff on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-strings-to-floats-in-pandas-dataframe
How to Convert String to Float in Pandas DataFrame - GeeksforGeeks
July 15, 2025 - String values are not very useful in data analysis, but changing them to float will provide much more value in your data analysis project. In this tutorial, we have covered the DataFrame.astype() and pandas.to_numeric() functions to convert ...
Discussions

pandas - How to convert a string to a float in my dataframe? (Python) - Stack Overflow
I'm working with my dataframe in Python. Dataframe looks like this: Timestamp cpu_system Host 2018-01-09 20:03:22 1.3240749835968018 pwp2 2017-09-30 21:03:22 ... More on stackoverflow.com
🌐 stackoverflow.com
Pandas DataFrame tries to convert a string into a float, while adding it to a column
Well you're using np.nan which is a float. Interestingly though, it only raises the error after the first item has been added. >>> df = pd.DataFrame() >>> df['foo'] = [np.nan] * len(df) >>> df Empty DataFrame Columns: [foo] Index: [] Note the type is float64 >>> df.dtypes foo float64 dtype: object However, pandas converts it. >>> df.at['file_path', 'foo'] = 'file1' >>> df.dtypes foo object dtype: object Second time round, it raises the error - not sure why that is exactly. >>> df['bar'] = [np.nan] * len(df) >>> df.dtypes foo object bar float64 dtype: object >>> df.at['file_path', 'bar'] = 'file1' Traceback (most recent call last): You can use an empty string '' instead of np.nan - or you could initialize your dataframe with values. You may also want to check out pathlib from pathlib import Path file_paths = ... df = pd.DataFrame({ Path(p).stem: [p] for p in file_paths }) .stem from pathlib is the filename without the extension. More on reddit.com
🌐 r/learnpython
4
2
December 13, 2021
python - How to convert string into float value in the dataframe - Stack Overflow
We are facing an error when we have a column which have datatype as string and the value like col1 col2 1 .89 So, when we are using def azureml_main(dataframe1 = None, dataframe2 = None)... More on stackoverflow.com
🌐 stackoverflow.com
April 25, 2017
How to convert a string into a float with a pandas dataframe?
apply(eval) is correct, although I don't see why you should need it twice. You don't with the one example you gave: >>> df = pd.DataFrame(['1/350', '5/45'], columns=["Meth"]) >>> df["Meth"].apply(eval) 0 0.002857 1 0.111111 Can you show us some more example data? More on reddit.com
🌐 r/learnpython
3
5
August 1, 2016
🌐
Statology
statology.org › home › how to convert strings to float in pandas
How to Convert Strings to Float in Pandas
November 28, 2022 - #convert both "assists" and "rebounds" from strings to floats df[['assists', 'rebounds']] = df[['assists', 'rebounds']].astype(float) #view column data types df.dtypes points float64 assists float64 rebounds float64 dtype: object · The following syntax shows how to convert all of the columns in the DataFrame to floats:
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › convert string to float in pandas dataframe column in python (4 examples)
Convert String to Float in pandas DataFrame Column in Python (Example)
May 2, 2022 - This example shows how to convert only one specific variable in a pandas DataFrame to the float data class. For this task, we can use the astype function as shown in the following Python code:
🌐
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
🌐
YouTube
youtube.com › watch
Converting String to Float in Python DataFrames - YouTube
Learn how to convert string data to float in Python DataFrames effortlessly. Discover essential tips for handling data type conversions and ensuring accurate...
Published: March 2, 2024
🌐
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 method has the format [dtype2 ... to help it make more sense. Strings can be converted to floats using the astype method with the Numpy data type numpy.float64:...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › convert-pandas-dataframe-column-to-float
Convert Pandas Dataframe Column To Float - GeeksforGeeks
July 23, 2025 - DataFrame.astype() method is used ... integers and strings and then we converted the string column to a float column using the astype() function....
🌐
Quora
quora.com › How-do-you-convert-a-string-to-a-float-in-pandas
How to convert a string to a float in pandas - Quora
Answer: Your question is not really clear. You want to convert a whole dataframe or a series ? If your talking about a single element, and assuming your using Python, you can just do something like this: a=”yourstring” yourfloat=float(a) finally you need to update your panda series, dataframe ...
🌐
Iditect
iditect.com › faq › python › converting-strings-to-floats-in-a-dataframe-in-python.html
Converting strings to floats in a DataFrame in python
To convert strings to floats in a DataFrame in Python using pandas, you can use the astype method or the pd.to_numeric function.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-convert-string-to-float
How to Convert String to Float in Python: Complete Guide with Examples | DigitalOcean
Learn how to convert strings to floats in Python using float(). Includes syntax, examples, error handling tips, and real-world use cases for data parsing.
🌐
Reddit
reddit.com › r/learnpython › pandas dataframe tries to convert a string into a float, while adding it to a column
r/learnpython on Reddit: Pandas DataFrame tries to convert a string into a float, while adding it to a column
December 13, 2021 -

Hello Guys,

I have a question regarding DataFrames. I have a line of code, which looks similar to this:

import os

import pandas as pd

import numpy as np

file_paths = ('C:/Users/DR/Documents/Polymer Science/Mitarbeiterpraktika/Forschungsmodul I Elektrochemie/Wasserspaltung/Co15-FTO_calc_WS_LS.txt', 'C:/Users/DR/Documents/Polymer Science/Mitarbeiterpraktika/Forschungsmodul I Elektrochemie/Wasserspaltung/Co16-FTO_calc_WS_LS.txt')

files_infos = pd.DataFrame()

for n, file_path in enumerate(file_paths) :

file_name = os.path.basename(file_path)

file_name = file_name.split(".txt")[0]

files_infos[file_name] = [np.nan] * len(files_infos)

files_infos.at["file_path", file_name] = file_path

If I run this script I get this Error. ValueError: could not convert string to float: 'C:/Users/DR/Documents/Polymer Science/Mitarbeiterpraktika/Forschungsmodul I Elektrochemie/Wasserspaltung/Co16-FTO_calc_WS_LS.txt'

I just don´t understand, why pandas tries to convert my string into a float. I thougt mabye it has something to do with the dtype of the DataFrame, but I couldn´t really find an answer (the dtype is object). What I find really confusing about this Error is, that I did use the same approach in different projects and it didn´t occur before.

Can someone of you mabye explain to me, why this error occurs and what I have to look up to find a solution? Please dont give me a solution to my problem, since I would like to solve it by myself in order to learn it.

Thank you for your help in advance.

🌐
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 - This will output the data types of each column in the DataFrame. If we have a column that is stored as a string, it will be displayed as object. Output: ... To convert a column from a string to a float, we can use the astype() method.
🌐
datagy
datagy.io › home › pandas tutorials › data analysis in pandas › converting pandas dataframe column from object to float
Converting Pandas DataFrame Column from Object to Float • datagy
May 7, 2023 - By the end of this tutorial, you’ll ... a Pandas DataFrame column’s data type from object (or string) to float is to use the astype method....
🌐
iO Flood
ioflood.com › blog › convert-string-to-float-python
Convert a String to a Float in Python: 3 Easy Methods
December 11, 2023 - So, let’s dive into to String conversion in Python! You can convert a string to a float in Python using the float() function and the syntax, number = float(data).
🌐
Arab Psychology
scales.arabpsychology.com › home › how can strings be converted to float in pandas?
How Can Strings Be Converted To Float In Pandas?
April 17, 2024 - In Pandas, this can be achieved by using the astype() method, which allows users to specify the desired data type for a particular column or series. By using this method, strings can be easily converted to float values, making them suitable ...
🌐
Reddit
reddit.com › r/learnpython › how to convert a string into a float with a pandas dataframe?
r/learnpython on Reddit: How to convert a string into a float with a pandas dataframe?
August 1, 2016 -

I have a column Column1 in a pandas dataframe which is of type str, values which are in the following form:

import pandas as pd
df = pd.read_table("filename.dat")
type(df["Column1"].ix[0])   #outputs 'str'
print(df["Column1"].ix[0])

which outputs '1/350'. So, this is currently a string. I would like to convert it into a float.

I tried this:

df["Column1"] = df["Column1"].astype('float64', raise_on_error = False)

But this didn't change the values into floats.

This also failed:

df["Column1"] = df["Column1"].convert_objects(convert_numeric=True)

And this failed:

df["Column1"] = df["Column1"].apply(pd.to_numeric, args=('coerce',))

How do I convert all the values of column "Column1" into floats? Could I somehow use regex to remove the parentheses?

This line works!

df["Column1"] = df["Column1"].apply(eval)

But it works only if I use it twice, i.e.

df["Column1"] = df["Column1"].apply(eval)
df["Column1"] = df["Column1"].apply(eval)

Why would this be?