Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing NAN's from numpy 2-D arrays

Tags:

python

nan

numpy

Similar to this question I would like to remove some NAN's from a 2-D numpy array. However, instead of removing an entire row that has NAN's I want to remove the corresponding element from each row of the array. For example (using list format for simplicity)

x=[ [1,2,3,4],
    [2,4,nan,8],
    [3,6,9,0] ]

would become

x=[ [1,2,4],
    [2,4,8],
    [3,6,0] ]

I can imagine using a numpy.where to figure out where in each row the NAN's appear and then use some loops and logic statements to make a new array from the old array (skipping over the NAN's and the corresponding elements in the other rows) but that to me doesn't seem to be a very streamlined way to do things. Any other ideas?

like image 656
NeutronStar Avatar asked Apr 03 '15 19:04

NeutronStar


People also ask

How do I delete a NaN row in NumPy?

Remove rows containing missing values ( NaN ) To remove rows containing missing values, use any() method that returns True if there is at least one True in ndarray . With the argument axis=1 , any() tests whether there is at least one True for each row.

How do I get rid of NANS in Python?

To remove NaN from a list using Python, the easiest way is to use the isnan() function from the Python math module and list comprehension. You can also use the Python filter() function. The Python numpy module also provides an isnan() function that we can use to check if a value is NaN.


1 Answers

You could use boolean indexing to select only those columns which do not contain nan:

>>> x[:, ~np.isnan(x).any(axis=0)]
array([[ 1.,  2.,  4.],
       [ 2.,  4.,  8.],
       [ 3.,  6.,  0.]])

(This is nearly identical to the answer you've linked to; only the axes have been switched.)

like image 172
Alex Riley Avatar answered Sep 29 '22 11:09

Alex Riley