Delete row if next row has the same first value, python

Question:

I have an array that looks like this:

data([0.000, 1], [0.0025, 2], [0.0025, 3], [0.005, 5])

I need to delete [0.0025, 3], because it has the same first value as the one before.

I have tried:

for i in data:
    if data[i, 0] == data[i+1,0]:
        np.delete(data, (i+1), axis = 0)

But then I get the following Error:

IndexError: arrays used as indices must be of integer (or boolean) type,

Can somebody help me with that

Asked By: vinc00

||

Answers:

input:

data = np.array([[0.000, 1], [0.0025, 2], [0.0025, 3], [0.005, 5]])

solution:

data = data[np.unique(data[:,0], return_index=True)[1]]

output:

array([[0.0e+00, 1.0e+00],
       [2.5e-03, 2.0e+00],
       [5.0e-03, 5.0e+00]])
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.