将对象dtype列转换为float或int

时间:2019-07-19 18:00:19

标签: python dtype

我正在尝试对某些数据进行聚类,但是当我尝试运行k-means时出现错误:float()参数必须是字符串或数字,而不是'Point'

enter image description here

我认为我需要更改dtype

我的熊猫df给了我以下dtypes:

Trajectory ID                                  object
Ordinal of the point within the trajectory    float64
Timestamp                                      object
Latitude                                      float64
Longitude                                     float64
dtype: object

我需要将对象转换为float或将int转换为k-means聚类算法

1 个答案:

答案 0 :(得分:1)

  

scikit-learn中有一个帮助程序类,可以很好地实现此目的,   它叫做sklearn.preprocessing.LabelEncoder

使用此类的fit_transform函数对字符串进行编码,并使用inverse_transform返回原始字符串