6

I have a NumPy matrix like the one below:

[[182 93 107 ..., nan nan -1]
 [182 93 107 ..., nan nan -1]
 [182 93 110 ..., nan nan -1]
 ..., 
 [188 95 112 ..., nan nan -1]
 [188 97 115 ..., nan nan -1]
 [188 95 112 ..., nan nan -1]]

I want to remove the columns which only involve nan values from the above matrix.

How can I do this? Thanks.

1 Answer 1

9

Assuming your array is of floats now, you can identify all the columns which are NaN and use fancy indexing to retrieve the others:

d
array([[ 182.,   93.,  107.,   nan,   nan,   -1.],
       [ 182.,   93.,  107.,    4.,   nan,   -1.],
       [ 182.,   93.,  110.,   nan,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   nan,   -1.],
       [ 188.,   97.,  115.,   nan,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   nan,   -1.]])


d[:,~np.all(np.isnan(d), axis=0)]

array([[ 182.,   93.,  107.,   nan,   -1.],
       [ 182.,   93.,  107.,    4.,   -1.],
       [ 182.,   93.,  110.,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   -1.],
       [ 188.,   97.,  115.,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   -1.]])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I have deleted my upper comment :)

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.