1. If the dataframe (say df) wholly consists of float64 dtypes, you can do:
df = df.astype('float32')
  1. Only if some columns are float64, then you'd have to select those columns and change their dtype:
# Select columns with 'float64' dtype  
float64_cols = list(df.select_dtypes(include='float64'))

# The same code again calling the columns
df[float64_cols] = df[float64_cols].astype('float32')
Answer from Shiva Govindaswamy on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 78625587 › fail-to-convert-float-64-to-float-32-in-python
pandas - Fail to convert float 64 to float 32 in python - Stack Overflow
from torch import nn import torch import pandas as pd import numpy as np df = pd.DataFrame([[1.1, 2.1], [3.1, 4.1],[5.1,6.1]], columns=['col1', 'col2']) df.astype('float32') print(df.dtypes) df.astype(np.float32) print(df.dtypes) a=torch.tensor(df.loc[1][['col1']]) a.to(torch.float32) print(a) ... col1 float64 col2 float64 dtype: object col1 float64 col2 float64 dtype: object tensor([3.1000], dtype=torch.float64)
🌐
GitHub
github.com › pandas-dev › pandas › issues › 15970
diff breaks when float64 column cast to float32 · Issue #15970 · pandas-dev/pandas
April 10, 2017 - Code Sample, a copy-pastable example if possible # x is a DataFrame (x['time'].iloc[0], x['time'].dtype, np.allclose(x['time'], x['time'].astype('float32')), np.allclose(x['time'].diff(), x['time'].astype('float32').diff(), equal_nan=Tru...
Author   jonathanstrong
🌐
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 - You can use the Pandas DataFrame.astype() function to convert a column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to a 54-bit signed float, you can use numpy.float64, ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_numeric.html
pandas.to_numeric — pandas 3.0.1 documentation - PyData |
>>> s = pd.Series([1, 2, 3], dtype="Int64") >>> pd.to_numeric(s, downcast="integer") 0 1 1 2 2 3 dtype: Int8 >>> s = pd.Series([1.0, 2.1, 3.0], dtype="Float64") >>> pd.to_numeric(s, downcast="float") 0 1.0 1 2.1 2 3.0 dtype: Float32
🌐
IncludeHelp
includehelp.com › python › how-to-convert-numpy-array-type-and-values-from-float64-to-float32.aspx
Python - How to convert NumPy array type and values from Float64 to Float32?
# Import numpy import numpy as np # Creating a numpy array arr = np.ones(4,dtype="float64") # Display original array print("Original Array:\n",arr,"\n") # Display type of original array print("type of Original Array:\n",arr.dtype,"\n") # Converting array into float32 arr = np.float32(arr) # Display type of modified array print("type of modified array:\n",arr.dtype,"\n")
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-integers-to-floats-in-pandas-dataframe
How to Convert Integers to Floats in Pandas DataFrame? | GeeksforGeeks
August 25, 2020 - In the above example, we change the data type of column 'Weight' from 'int64' to 'float64'. Example 2: Converting more than one column from int to float using DataFrame.astype() ... # importing pandas library import pandas as pd # Initializing the nested list with Data set player_list = [['M.S.Dhoni', 36, 75, 5428000, 176], ['A.B.D Villers', 38, 74, 3428000, 175], ['V.Kholi', 31, 70, 8428000, 172], ['S.Smith', 34, 80, 4428000, 180], ['C.Gayle', 40, 100, 4528000, 200], ['J.Root', 33, 72, 7028000, 170], ['K.Peterson', 42, 85, 2528000, 190]] # creating a pandas dataframe df = pd.DataFrame(player_list, columns=[ 'Name', 'Age', 'Weight', 'Salary', 'Strike_rate']) # lets find out the data type of 'Age' # and 'Strike_rate' columns print(df.dtypes)
Find elsewhere
🌐
Python Forum
python-forum.io › thread-2181.html
pandas convert to tuple & float to float64
Dear Pandas Experts, I got two question on my Introduction to Python homework. The jupiter auto-grader expects in case 1 a float64 and in case 2 a tuple, not a list. case 1 newfour_2['GDPDiff']=np.subtract(newfour['2015'],newfour['2006']) ...
🌐
Jasonblog
jasonblog.github.io › note › python › convert_list_of_numpyfloat64_to_float_and_converti.html
Convert list of numpy.float64 to float and Converting strings to floats in a DataFrame | Jason note
Jason note · Convert list of numpy.float64 to float and Converting strings to floats in a DataFrame · import pandas as pd h = pd.Series(['15', '21.0', '33.0']) l = pd.Series(['1', '2.0', '3.0']) # Converting strings to floats in a DataFrame using to_numeric h = pd.to_numeric(h) l = ...
🌐
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-41.php
NumPy: Convert numpy dtypes to native python types - w3resource
August 28, 2025 - This problem involves writing a NumPy program to convert NumPy data types (dtypes) to native Python types. The task requires using NumPy's type conversion functions to transform NumPy-specific data types, such as numpy.int32 or numpy.float32, into their equivalent native Python types, like int or float.
🌐
Skytowner
skytowner.com › explore › converting_column_type_to_float_in_pandas_dataframe
Converting column type to float in Pandas DataFrame
To convert the column type to float in Pandas DataFrame, either use the Series' astype() method, or use Pandas' to_numeric() method.
🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - >>> arr = np.arange(0, 8388608, 0.5, dtype=np.float64) >>> arr[-4:] array([8388606. , 8388606.5, 8388607. , 8388607.5]) >>> arr[-4:].astype(np.float32) array([8388606. , 8388606.5, 8388607.
🌐
CodeSignal
codesignal.com › learn › courses › cleaning-and-transforming-data-with-pandas › lessons › data-type-conversion-in-pandas
Data Type Conversion in Pandas
Pandas provides a powerful method, astype, to transform the data type of a column swiftly and efficiently. Let’s see how astype can be utilized: ... In this segment, the conversion ensures that the 'Age' column changes to integer and the 'Salary' column to float. Now, let's delve deeper into understanding the difference between int32, float32 and int64, float64. When converting ...
Top answer
1 of 7
49

You can convert most of the columns by just calling convert_objects:

In [36]:

df = df.convert_objects(convert_numeric=True)
df.dtypes
Out[36]:
Date         object
WD            int64
Manpower    float64
2nd          object
CTR          object
2ndU        float64
T1            int64
T2          int64
T3           int64
T4        float64
dtype: object

For column '2nd' and 'CTR' we can call the vectorised str methods to replace the thousands separator and remove the '%' sign and then astype to convert:

In [39]:

df['2nd'] = df['2nd'].str.replace(',','').astype(int)
df['CTR'] = df['CTR'].str.replace('%','').astype(np.float64)
df.dtypes
Out[39]:
Date         object
WD            int64
Manpower    float64
2nd           int32
CTR         float64
2ndU        float64
T1            int64
T2            int64
T3            int64
T4           object
dtype: object
In [40]:

df.head()
Out[40]:
        Date  WD  Manpower   2nd   CTR  2ndU   T1    T2   T3     T4
0   2013/4/6   6       NaN  2645  5.27  0.29  407   533  454    368
1   2013/4/7   7       NaN  2118  5.89  0.31  257   659  583    369
2  2013/4/13   6       NaN  2470  5.38  0.29  354   531  473    383
3  2013/4/14   7       NaN  2033  6.77  0.37  396   748  681    458
4  2013/4/20   6       NaN  2690  5.38  0.29  361   528  541    381

Or you can do the string handling operations above without the call to astype and then call convert_objects to convert everything in one go.

UPDATE

Since version 0.17.0 convert_objects is deprecated and there isn't a top-level function to do this so you need to do:

df.apply(lambda col:pd.to_numeric(col, errors='coerce'))

See the docs and this related question: pandas: to_numeric for multiple columns

2 of 7
41

convert_objects is deprecated.

For pandas >= 0.17.0, use pd.to_numeric

df["2nd"] = pd.to_numeric(df["2nd"])
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-float64-columns-to-int64-in-pandas
How to Convert float64 Columns to int64 in Pandas? - GeeksforGeeks
July 11, 2024 - This enables the conversion of a column from various data types such as float or string to an integer type, specifically int64 or int32. We can use astype() method in Python to convert float64 Columns to int64 in Pandas.