Compare an element of one matrix with the next one and if it is equal to eliminate the complete row in python

1

I have this matrix

  

and I need to delete the rows that have the elements of the second column repeated

I need something like this:

  

I have this code:

for i in range(a,b):
    if lines3[i-1,1] == lines3[i,1]:
        lines4 = np.delete(lines3,i,0)

print(lines4)  

and it looks like this

[[1581 1243 2459 1260]
 [1581 1243 2459 1257]
 [1581 1244 2459 1260]
 [1581 2018 2459 2032]]
    
asked by Fatima 20.05.2017 в 00:21
source

1 answer

0

You do not need to reinvent the wheel in principle, there's something in Numpy that does that for you, numpy.unique . Returns the unique elements of an array, but if we pass the parameter index as True then returns a second array containing the indexes of those elements, and if there is more than one returns the index of the first found.

Applying it over the column in question (the second column of your matrix) you only need to do slicing later on your matrix using that array.

In your case there is a problem, you want the last row in the final matrix that has the repeated column and not the first one. You could make the matrix be ordered in the appropriate way, something like this:

[[1581  1243  2459  1257]
 [1581  1243  2459  1259]
 [1581  1243  2459  1260] 
 [1581  1244  2459  1260] 
 [1581  2018  2459  2032]]

However, as you have it, we can use another parameter of numpy.unique , return_counts that gives us the times that these elements appear repeated and recalculate the indexes:

import numpy as np

matriz = np.array([[1581,  1243,  2459,  1260], 
                   [1581,  1243,  2459,  1259], 
                   [1581,  1243,  2459,  1257], 
                   [1581,  1244,  2459,  1260], 
                   [1581,  2018,  2459,  2032]])

_, indices, counts = np.unique(matriz[:,1], return_index=True,  return_counts = True)
filtro = counts + indices - 1
matriz = matriz[filtro, :]

print(matriz)

Exit:

[[1581 1243 2459 1257]
 [1581 1244 2459 1260]
 [1581 2018 2459 2032]]

Warning: In order for this code to work properly, your matrix must be previously sorted in the same way as yours. That is, the matrix has to be previously sorted so that the rows with the same element in the second column are together and the last of each group is the one you want in your final matrix.

    
answered by 20.05.2017 / 01:53
source