0

I have an array of the shape (1179648, 909). The problem is that some rows are filled with 0's only. I am checking for this as follows:

for i in range(spectra1Only.shape[0]):
    for j in range(spectra1Only.shape[1]):
        if spectra1Only[i,j] == 0:

I now want to remove the whole row of [i] if there is any 0 appearing to get a smaller amount of only the data needed.

My question is: what would be the best method to do so? Remove? Del? numpy.delete? Or any other method?

2
  • The problem is that some rows are filled with 0's only and remove the whole row of [i] if there is any 0 appearing are contradictory. do you want to remove a row when even a single value is 0 or when all values are 0? Commented Nov 22, 2018 at 9:42
  • 1
    shouldnt make a difference as if there is one 0 the whole row will be 0 Commented Nov 22, 2018 at 9:44

1 Answer 1

1

You can use Boolean indexing with np.any along axis=1:

spectra1Only = spectra1Only[~(spectra1Only == 0).any(1)]

Here's a demonstration:

A = np.random.randint(0, 9, (5, 5))

print(A)

[[5 0 3 3 7]
 [3 5 2 4 7]
 [6 8 8 1 6]
 [7 7 8 1 5]
 [8 4 3 0 3]]

print(A[~(A == 0).any(1)])

[[3 5 2 4 7]
 [6 8 8 1 6]
 [7 7 8 1 5]]
Sign up to request clarification or add additional context in comments.

1 Comment

wow thank you so much thats so much easier and totally solves what i needed! sorry but im a beginner and teaching things somehow by myself

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.