Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading