如何使用Python维持从NDARRAY到集合的数字的发生序列



方案

我正在尝试获取数据框架所属的簇数。其数据类型是<type 'numpy.ndarray'>和以下数据

records_Array = array([0, 0, 0, 0, 2, 2, 1, 1, 1], dtype=int32)

显然在打印时,我以这种格式看到[0 0 0 ..., 1 1 1]

现在,我只需要一次数字,所以我转换为集合,然后列表,

cluster_set = list(set(records_Array))

输出

在打印cluster_set上,我得到[0, 1, 2]

0, 2, 1

必需

我需要一些功能/方法,它保留了records_Array的顺序并返回cluster_set

您想要pandas' pd.unique,因为它没有找到唯一的值。Numpy的唯一函数确实可以。

a = np.array([0, 0, 0, 0, 2, 2, 1, 1, 1])
pd.unique(a)
array([0, 2, 1])

最新更新