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

CopyIn [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
🌐
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
Discussions

python - pandas convert strings to float for multiple columns in dataframe - Stack Overflow
I'm new to pandas and trying to figure out how to convert multiple columns which are formatted as strings to float64's. Currently I'm doing the below, but it seems like apply() or applymap() shoul... More on stackoverflow.com
🌐 stackoverflow.com
How can I parse values to float in a pandas dataframe column that contains both floats and strings?
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_numeric.html df_distns['Parameter 3'] = pd.to_numeric(df_distns['Parameter 3'], errors='ignore') More on reddit.com
🌐 r/learnpython
7
0
June 22, 2021
python - pandas how to convert all the string value to float - Stack Overflow
I want to convert all the string value in Pandas DataFrame into float, and I can define a short function to do this, but it's not a Pythonic way to do that. My DataFrame looks like this: >>&... 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
People also ask

How do I convert a pandas column to float?
You can convert a pandas column to float using the astype() method. Example: df['column'] = df['column'].astype(float). This changes the column data type to float.
🌐
golinuxcloud.com
golinuxcloud.com › home › databases › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & ...
How do I convert all columns in a pandas DataFrame to float?
You can convert all columns using apply(). Example: df = df.apply(pd.to_numeric, errors='coerce') which converts columns to numeric values including float.
🌐
golinuxcloud.com
golinuxcloud.com › home › databases › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & ...
How do I convert an object column to float in pandas?
Object columns can be converted to float using pd.to_numeric() or astype(float). Example: df['column'] = pd.to_numeric(df['column']). This safely converts string values to numeric format.
🌐
golinuxcloud.com
golinuxcloud.com › home › databases › convert pandas dataframe column to float (astype, to_numeric & practical examples)
Convert pandas DataFrame Column to Float (astype, to_numeric & ...
🌐
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 - Example: In this example, we'll convert each value of the 'Inflation Rate' column to float. ... # importing pandas library import pandas as pd # creating a dictionary Data = {'Year': ['2016', '2017', '2018', '2019'], 'Inflation Rate': ['4.47', '5', '5.98', '4.1']} # create a dataframe df = pd.DataFrame(Data) # converting each value of column to a string df['Inflation Rate'] = pd.to_numeric(df['Inflation Rate']) # show the dataframe print(df) # show the data types print (df.dtypes)
🌐
Statology
statology.org › home › how to convert strings to float in pandas
How to Convert Strings to Float in Pandas
November 28, 2022 - You can use the following methods to convert a string to a float in pandas: Method 1: Convert a Single Column to Float · #convert "assists" column from string to float df['assists'] = df['assists'].astype(float) Method 2: Convert Multiple Columns to Float ·
🌐
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 to cast a Pandas object to a specified dtype. astype() function is used to convert a particular column data type to another data type. Here, we created a sample data frame with two columns containing integers ...
Find elsewhere
🌐
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.
🌐
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 - After executing the previous syntax, a new pandas DataFrame called data_new1 has been created. Let’s print the classes of all columns of this new data set: print(data_new1.dtypes) # Check data types of columns # x1 float64 # x2 object # x3 object # dtype: object · As you can see, we have changed the data type of the column x1 from object (or string) to the float64 class. It is also possible to transform multiple pandas DataFrame columns to the float data type.
🌐
Skytowner
skytowner.com › explore › converting_column_type_to_float_in_pandas_dataframe
Converting column type to float in Pandas DataFrame
Note that Pandas will only allow columns containing NaN to be of type float. To convert the data type of multiple columns to float, use Pandas' apply(~) method with to_numeric(~).
🌐
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 - Project columns have all been converted from Pandas strings to floats using the astype function.
🌐
Reddit
reddit.com › r/learnpython › how can i parse values to float in a pandas dataframe column that contains both floats and strings?
r/learnpython on Reddit: How can I parse values to float in a pandas dataframe column that contains both floats and strings?
June 22, 2021 -

I have a pandas dataframe, populated using pandas.readcsv. Most of the values in the csv file are numeric, but there is one column of the csv, 'Parameter 3', which contains a couple of string values. Because of these 2 string values, every other value in this column is being interpreted as a string too.

I can deal with this using the following code, but it is not very elegant;

for i in df_distns.index:

    try:

        df_distns.loc[i, 'Parameter 3'] = float(df_distns.loc[i, 'Parameter 3'])

    except:

        pass  

Can anyone recommend a nicer way to do this (without looping)?

Top answer
1 of 2
12

Assuming all values can be correctly converted to float, you can use DataFrame.astype() function to convert the type of complete dataframe to float. Example -

df = df.astype(float)

Demo -

In [5]: df = pd.DataFrame(np.array([['1', '2', '3'], ['4', '5', '6']]))

In [6]: df.astype(float)
Out[6]:
   0  1  2
0  1  2  3
1  4  5  6

In [7]: df = df.astype(float)

In [8]: df.dtypes
Out[8]:
0    float64
1    float64
2    float64
dtype: object

.astype() function also has a raise_on_error argument (which defaults to True) which you can set to False to make it ignore errors . In such cases, the original value is used in the DataFrame -

In [10]: df = pd.DataFrame([['1', '2', '3'], ['4', '5', '6'],['blah','bloh','bleh']])

In [11]: df.astype(float,raise_on_error=False)
Out[11]:
      0     1     2
0     1     2     3
1     4     5     6
2  blah  bloh  bleh

To convert just a series/column to float, again assuming all values can be converted, you can use [Series.astype()][2] . Example -

df['somecol'] = df['somecol'].astype(<type>)
2 of 2
6

Another option is to use df.convert_objects(numeric=True). It attempts to convert numeric strings to numbers, with unconvertible values becoming NaN:

import pandas as pd

df = pd.DataFrame([['1', '2', '3'], ['4', '5', 'foo'], ['bar', 'baz', 'quux']])
df = df.convert_objects(convert_numeric=True)
print(df)

yields

    0   1   2
0   1   2   3
1   4   5 NaN
2 NaN NaN NaN

In contrast, df.astype(float) would raise ValueError: could not convert string to float: quux since in the above DataFrame some strings (such as 'quux') is not numeric.

Note: in future versions of pandas (after 0.16.2) the function argument will be numeric=True instead of convert_numeric=True.

🌐
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.

🌐
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 - One of the common tasks in data analysis is converting strings to float values, which is often necessary for performing mathematical operations. In Pandas, this can be achieved by using the astype() method, which allows users to specify the ...
🌐
Reddit
reddit.com › r/learnpython › python pandas - can't convert string to float (i think b/c of multiple data types in column...)
r/learnpython on Reddit: Python pandas - can't convert string to float (I think b/c of multiple data types in column...)
July 17, 2022 -

I want to do some math on a dataframe but (I think) can't get one column/series into the necessary format. The column contains strings; some are '.123' while others are '0'. When I attempt the math on the column of strings by converting everything to an integer like so:

dfteam1['cloff'] = dfteam1.cloff.astype(int)

I get the following error

ValueError: invalid literal for int() with base 10: '.123'

I think it's b/c .123 isn't an integer but a float, so I change the code like so:

dfteam1['cloff'] = dfteam1.cloff.astype(float)

now I get the following error

ValueError: could not convert string to float:

I think it's b/c 0 isn't a float but an integer? Do I need to change all the 0 values to 0.00 or am I completely off base? All feedback is welcome.