原数组
arr = np.array([[4,2],[3,2],[0,1],[2,2]])
arr
>>>array([[4, 2],
>>>
[3, 2],
>>>
[0, 1],
>>>
[2, 2]])
numpy.unique() 打破原顺序
np.unique(arr)
>>> array([0, 1, 2, 3, 4])
pandas的unique() 保持原顺序
choices = pd.DataFrame(arr)[0].unique()
choices
>>> array([4, 3, 0, 2])
#建立映射关系
map_value = range(0, len(choices))
map_value
>>> range(0, 4)
map_dict = pd.Series(index=choices, data=map_value)
map_dict
>>> 4
0
>>> 3
1
>>> 0
2
>>> 2
3
>>> dtype: int64
最后
以上就是玩命橘子最近收集整理的关于去掉重复元素,提取数组/集合中的单独元素——nump和pandas的unique,如何保持数据原顺序。原数组的全部内容,更多相关去掉重复元素,提取数组/集合中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复