Python 如何转换数据帧中所有列的数据类型

Python 如何转换数据帧中所有列的数据类型,python,pandas,Python,Pandas,我有200多列的熊猫数据框。所有列都是int类型。我需要将它们转换为float类型。我找不到做这件事的方法。 我试过了 但是在for循环之后,当我打印X_data.dtypes时,所有列仅显示为int。 我还尝试了X\u data=X\u data.apply(pd.to\u numeric),但它没有转换为float 数据框由csv文件加载构建。如果要将特定列转换为特定类型,可以使用: new_type_dict = { 'col1': float,

我有200多列的熊猫数据框。所有列都是int类型。我需要将它们转换为float类型。我找不到做这件事的方法。 我试过了

但是在for循环之后,当我打印
X_data.dtypes
时,所有列仅显示为int。 我还尝试了
X\u data=X\u data.apply(pd.to\u numeric)
,但它没有转换为float


数据框由csv文件加载构建。

如果要将特定列转换为特定类型,可以使用:

new_type_dict = {
                 'col1': float, 
                 'col2': float
                 } 

df = df.astype(new_type_dict) 
它现在将所选列转换为新类型


我在

中找到了它,如果您想将特定列转换为可以使用的特定类型:

new_type_dict = {
                 'col1': float, 
                 'col2': float
                 } 

df = df.astype(new_type_dict) 
它现在将所选列转换为新类型


我是从

中找到的,这些值没有保存到位。请尝试以下操作:

for column in X_data:
    X_data[column] = X_data[column].astype('float64')

这些值未保存到位。请尝试以下操作:

for column in X_data:
    X_data[column] = X_data[column].astype('float64')
X_data=X_data.astype(float)
X_data=X_data.astype(float)