numpy remove columns containing nan 1

numpy remove columns containing nan

a = a[~(np.isnan(a).any(axis=0))] # removes columns containing at least one nan
a = a[~(np.isnan(a).all(axis=0))] # removes columns containing all nan

Here is what the above code is Doing:
1. np.isnan(a) returns a boolean array of the same shape as a, where True indicates a NaN value.
2. np.isnan(a).any(axis=0) returns a boolean array of shape (a.shape[1],) where True indicates that the column contains at least one NaN value.
3. np.isnan(a).any(axis=0) returns a boolean array of shape (a.shape[1],) where True indicates that the column contains at least one NaN value.
4. np.isnan(a).all(axis=0) returns a boolean array of shape (a.shape[1],) where True indicates that the column contains all NaN values.
5. ~(np.isnan(a).any(axis=0)) returns a boolean array of shape (a.shape[1],) where True indicates that the column contains no NaN values.
6. a[~(np.isnan(a).any(axis=0))] returns a new array with the columns where True in the previous step.
7. a[~(np.isnan(a).all(axis=0))] returns a new array with the columns where False in the previous step.

Similar Posts