[Numpy-discussion] how to delete a particular column or row from numpy array based on some rule or value

Derek Homeier derek at astro.physik.uni-goettingen.de
Thu Mar 24 09:33:55 EDT 2011


> In a numpy array of m x n size, I would like to delete few rows when  
> a given element in that row is ‘nan’ or say any other value.
>
>
> For e.g.  as in example given below, if I wish to delete a row when  
> the 3rd element of row is zero, or if 3rd, 4th, 5th element are zero  
> or either of 3rd, 4th and 5th element is zero.
>
> array([[ 1900. ,     nan,     nan,     nan,     nan,     nan],
>
>        [ 1900.5,     nan,     nan,     nan,     nan,     nan],
>
>        [ 1901. ,     nan,     nan,     nan,     nan,     nan],
>
>        ...,
>
>        [ 6724. ,     nan,     nan,     nan,     nan,     nan],
>
>        [ 6724.5,     nan,     nan,     nan,     nan,     nan],
>
>        [ 6725. ,     nan,     nan,     nan,     nan,     nan]])
>
> Cheers
>
>
> Sachin
>
>
>

> I think u can use  numpy.loadtext command
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html
>
>
> -- 
> DILEEPKUMAR. R
> J R F, IIT DELHI

Wouldn't that require to transform the array to a text representation  
first?
If you have an existing array, you can use the numpy test and logical
functions to index it. E.g.
myarr[:,2] == 0    selects the rows with 3rd column zero,
np.isnan(myarr[:,3])   those with 4th column NaN
and to select only rows where neither is the case:

clean = myarr[np.logical_not( (myarr[:,2]==0) | (np.isnan(darr[:,3])) )]

- combine as required with the '&' or '|' operators (there does not seem
to be a shorthand for logical_not...), and check np.isnan, np.isinf,  
np.isfinite -
those should allow you to select what you need.

HTH,
						Derek




More information about the NumPy-Discussion mailing list