场景
我正在尝试获取数据帧所属的集群数。
其数据类型为<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]
where as the clusters are in sequence of
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])
关于python - 如何使用python维护数组中数字出现的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47428707/