0

I have a numpy array (type numpy.ndarray) where a few rows have missing values (all missing values to be precise). How do I remove a row from the array if it contains missing values?

1 Answer 1

3

Use np.isfinite in combination with np.any or np.all with the axis argument.

a = np.round(np.random.normal(size=(5, 3)), 1)
a[1, 2] = np.nan
a[2] = np.nan

print(a)
print(a[np.all(np.isfinite(a), axis=1)])  # Removes rows with any non-finite values.
print(a[np.any(np.isfinite(a), axis=1)])  # Removes rows that are all non-finite.
Sign up to request clarification or add additional context in comments.

Comments

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.