How to delete all lines in numpy.ndarray that contain non-numeric values

Basically, I do data analysis. I read in the dataset as numpy.ndarray, and some of the values ​​are missing (either just not being there, being NaN , or being a string written " NA ").

I want to clear all lines containing any entry like this. How to do it with numpy ndarray?

+48
python numpy
Jul 12 2018-12-12T00:
source share
1 answer
 >>> a = np.array([[1,2,3], [4,5,np.nan], [7,8,9]]) array([[ 1., 2., 3.], [ 4., 5., nan], [ 7., 8., 9.]]) >>> a[~np.isnan(a).any(axis=1)] array([[ 1., 2., 3.], [ 7., 8., 9.]]) 

and reassign it to a .

Explanation: np.isnan(a) returns a similar array with True , where NaN , False elsewhere. .any(axis=1) reduces the array m*n to n with the logical operation or in all lines, ~ inverts True/False and a[ ] selects only lines from the original array that have True in brackets.

+92
Jul 12 2018-12-12T00:
source share
β€” -



All Articles