[SciPy-User] [scipy-user] How to remove a value from an np array?

Warren Weckesser warren.weckesser@enthought....
Fri Jan 27 11:02:20 CST 2012


On Fri, Jan 27, 2012 at 10:56 AM, Fabien Lafont <lafont.fabien@gmail.com>wrote:

> thanks a lot. Is there a way to do so but acting on elements array? I
> explain you why. I have a vector with some 'nan' elements. I want to
> remove them and to know the position on the array to remove another
> element on another array but at the same 'place(position number)'
>
>

Perhaps something like this:

In [29]: a
Out[29]: array([  1.,   2.,  nan,   4.,  nan])

In [30]: b
Out[30]: array([10, 20, 30, 40, 50])

In [31]: clean_a = a[isfinite(a)]

In [32]: clean_b = b[isfinite(a)]

In [33]: clean_a
Out[33]: array([ 1.,  2.,  4.])

In [34]: clean_b
Out[34]: array([10, 20, 40])


If you really need the indices of the values that are not nan, you can use
the 'where' function:

In [35]: where(isfinite(a))
Out[35]: (array([0, 1, 3]),)


Warren




>
> 2012/1/27 Warren Weckesser <warren.weckesser@enthought.com>:
> >
> >
> > On Fri, Jan 27, 2012 at 10:49 AM, Fabien Lafont <lafont.fabien@gmail.com
> >
> > wrote:
> >>
> >> I have that [3,2,4,8,7,8,9] and I want [3,2,8,7,8] how can I do?  I've
> >> tried remove() but it works only on lists not on np.array.
> >
> >
> >
> > In [21]: a = array([3,2,4,8,7,8,9])
> >
> > In [22]: b = a[(a != 4) & (a != 9)]
> >
> > In [23]: b
> > Out[23]: array([3, 2, 8, 7, 8])
> >
> >
> > Warren
> >
> >
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User@scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> >
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120127/c3543193/attachment-0001.html 


More information about the SciPy-User mailing list