删除numpy数组的重复行 [英] Remove duplicate rows of a numpy array

查看:135
本文介绍了删除numpy数组的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除二维numpy数组的重复行?

How can I remove duplicate rows of a 2 dimensional numpy array?

data = np.array([[1,8,3,3,4],
                 [1,8,9,9,4],
                 [1,8,3,3,4]])

答案应该如下:

ans = array([[1,8,3,3,4],
             [1,8,9,9,4]])

如果有两行相同,那么我想删除一个重复"行.

If there are two rows that are the same, then I would like to remove one "duplicate" row.

推荐答案

您可以使用numpy unique.由于您需要唯一的行,因此我们需要将它们放入元组:

You can use numpy unique. Since you want the unique rows, we need to put them into tuples:

import numpy as np

data = np.array([[1,8,3,3,4],
                 [1,8,9,9,4],
                 [1,8,3,3,4]])

仅将np.unique应用于data 数组将导致以下结果:

just applying np.unique to the data array will result in this:

>>> uniques
array([1, 3, 4, 8, 9])

打印出列表中的唯一元素 .因此,将它们放入元组会导致:

prints out the unique elements in the list. So putting them into tuples results in:

new_array = [tuple(row) for row in data]
uniques = np.unique(new_array)

打印:

>>> uniques
array([[1, 8, 3, 3, 4],
       [1, 8, 9, 9, 4]])

这篇关于删除numpy数组的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆