Home > other >  Delete row if next row has the same first value, python
Delete row if next row has the same first value, python

Time:11-26

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

CodePudding user response:

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]])
  • Related