okvoyce
okvoyce

Reputation: 153

Remove entire row of np array if there is duplicate in first column

I have the following test array:

arr = np.array([[1, 2, 3], [1,3,7], [2,1,3], [4, 5, 6], [1,4,7], [2,7,6])

I need to remove every row that has a duplicate value in the first column (but still keeping the first instance of that value). For this test array I require the following output:

result=[1,2,3],[2,1,3],[4,5,6]

So the first row where 1 is in the first column is kept, and the first row where 2 is in the column is kept etc...

Any help would be appreciated!

Upvotes: 0

Views: 34

Answers (1)

yatu
yatu

Reputation: 88276

return_index in np.unique is quite useful for this:

_, i = np.unique(arr[:,0], return_index=True)

arr[i]
array([[1, 2, 3],
       [2, 1, 3],
       [4, 5, 6]])

Upvotes: 1

Related Questions