1

I've been trying to write some code to delete rows from my 2d array according to the following criteria:

  1. every lone entry, so that no patient only has one entry (the mriindex ticks up by 1 for every entry of the same patient in the array)
  2. every entry above the 4th one.

Should either of those criteria be fulfilled, np.delete should remove the row currently being iterated through (i.e. the ith row of the index) The mriindex is the 6th column in my array.

Input for np.delete was the array arr, the row index i and the axis 0 (for row if I'm not mistaken), mapped to a new array new_arr.

As can be seen from the output though, my conditions aren't fulfilled. For example, the 4th person in the array (Alex Maier) should no longer be there (being a lone entry).

Help would be very much appreciated.

Code (very inefficient) is the following:

#remove single entries

i = 0
for i in range(n-1): 
    if arr[i][5] == 1:
        if arr[i+1][5] == 1:
            new_arr = np.delete(arr, i, axis = 0)
            i = i+1
    if arr[i][5]!=1:
        if arr[i][5] >4:
            new_arr = np.delete(arr, i , axis = 0)
            i = i+1
        else:
            i = i+1

code with output array

3
  • Show code and relevant output (maybe shortened) as properly formatted text in the question, not as image or external link. Commented Jul 14, 2022 at 17:58
  • Welcome to Stackoverflow. Please share your code and the results as text, to make it easier for others to help you. Commented Jul 14, 2022 at 17:58
  • You mean like this? Commented Jul 15, 2022 at 8:52

1 Answer 1

1

Have a look at your code and check what is happening to new_arr when the next loop iteration starts.

The approach you chose however is not very efficient, because you will copy the array every time you call delete. It is better do do it in one shot, something like this:

# creating bool arrays with the intended logic
lessThan5 = arr[:,5]<5
singleEntry = np.diff(arr[:,5],append=[1])!=0

# using np.where to filter
keepers = np.where(np.logical_and(lessThan5, singleEntry))

# index and save to new array
filtered_arr = arr[keepers[0],:]
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for answering! Currently, the np.diff looks at the difference between my current value of arr[:, 5] and the previous value. Then if this is >0, it returns TRUE. How would I change it so that it looks at the current value and the next value of arr[:5] instead? Because, at the moment, the code deletes the first entry in a sequence (i.e. from 1, 1, 2, 3, 4 the 2nd 1 is deleted, whereas the first one should be removed instead.)
Would changing np.diff(... , prepend = [0])>0 to (..., append=[1] !=0) work?
@DaveGordon Right, I think ..., append=[1]!)=0 should behave as you wish. Modified my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.