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.
Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np. isnan() function.
dropna() is used to drop/remove columns with NaN / None values.
How to drop all missing values from a numpy array? Droping the missing values or nan values can be done by using the function "numpy. isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy. logical_not()" where the boolean values will be reversed.
Assuming your array is of float
s 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.]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With