[英]How to convert datatype of all columns in a pandas dataframe
I have pandas dataframe with 200+ columns.我有 200 多列的熊猫数据框。 All the columns are of type int.
所有列都是 int 类型。 And I need to convert them to float type.
我需要将它们转换为浮点型。 I could not find a way to do it.
我找不到办法做到这一点。 I tried
我试过
for column in X_data:
X_data[column].astype('float64')
But after the for loop, when I print X_data.dtypes
, all columns show as int only.但是在 for 循环之后,当我打印
X_data.dtypes
,所有列仅显示为 int。 I also tried X_data = X_data.apply(pd.to_numeric)
but it did not convert to float.我也试过
X_data = X_data.apply(pd.to_numeric)
但它没有转换为浮点数。
The dataframe is constructed from a csv file load.数据框是从 csv 文件加载构建的。
The values aren't being saved in place.这些值没有保存到位。 Try the following:
请尝试以下操作:
for column in X_data:
X_data[column] = X_data[column].astype('float64')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.