remove nan values from np array

Viewed 35
my_list=[[  0.,  40. , nan],
 [60. ,  0. , nan],
 [ nan , nan , nan]]

Is it possible that I can remove the nan value?

Expected output:

my_list=[[0.,40.],
 [60., 0.]]
1 Answers
import numpy as np

x=np.array([[  0.,  40. , np.nan],
 [60. ,  0. , np.nan],
 [ np.nan , np.nan , np.nan]])

x = (x[~np.isnan(x).all(axis=1), :]) # remove rows with nan
x = (x[:, ~np.isnan(x).all(axis=0)]) # remove cols with nan

Output

[[ 0. 40.]
 [60.  0.]]

But as said @mozway if a row or column contains at least one not nan, then it will remain in the result.

Related