Removing columns that have only nan values ​​from a NumPy array

I have a matrix NumPyas shown 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 columns containing only values nanfrom the matrix above.

How can i do this? Thanks.

+4
source share
1 answer

Assuming your array has float, now you can identify all the columns that are NaN and use fancy indexing to fetch 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.]])
+4
source

Source: https://habr.com/ru/post/1627023/


All Articles