-1
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.]]
Jack
  • 181
  • 10
  • 5
    It's not always possible, imagine that 40 was NaN, what should be the output? Also, do you have a numpy array or a nested list? – mozway Sep 15 '22 at 08:58

1 Answers1

0
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.