Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing NaN rows from a three dimensional array

Tags:

python

numpy

How can I remove the NaN rows from the array below using indices (since I will need to remove the same rows from a different array.

array([[[nan,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]],

       [[ 0.,  0.,  0.,  0.],
        [ 0., nan,  0.,  0.],
        [ 0.,  0.,  0.,  0.]]])

I get the indices of the rows to be removed by using the command

a[np.isnan(a).any(axis=2)]

But using what I would normally use on a 2D array does not produce the desired result, losing the array structure.

a[~np.isnan(a).any(axis=2)]
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])

How can I remove the rows I want using the indices obtained from my first command?

like image 874
dearn44 Avatar asked Feb 24 '19 17:02

dearn44


People also ask

How to delete NaN values from array?

Remove rows containing missing values ( NaN )Use the negation operator ~ to make rows with no missing values True . By applying this boolean array to the first dimension (= row) of the original array, the rows containing the missing values are removed (= the rows containing the missing values are extracted).

How do I delete NaN?

By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .

How do you replace NaN values in an array with 0?

You can use numpy. nan_to_num : numpy. nan_to_num(x) : Replace nan with zero and inf with finite numbers.

How to remove NaN values from NumPy array?

Using np.isfinite Remove NaN values from a given NumPy Using this function we will get indexes for all the elements which are not nan. From the indexes, we can filter out the values that are not nan and save them in another array.


1 Answers

You need to reshape:

a[~np.isnan(a).any(axis=2)].reshape(a.shape[0], -1, a.shape[2])

But be aware that the number of NaN-rows at each 2D subarray should be the same to get a new 3D array.

like image 152
Andreas K. Avatar answered Oct 26 '22 22:10

Andreas K.