[Numpy-discussion] Remove duplicate columns

josef.pktd at gmail.com josef.pktd at gmail.com
Thu May 6 13:36:42 EDT 2010


On Thu, May 6, 2010 at 1:25 PM, T J <tjhnson at gmail.com> wrote:
> Hi,
>
> Is there a way to sort the columns in an array?  I need to sort it so
> that I can easily go through and keep only the unique columns.
> ndarray.sort(axis=1) doesn't do what I want as it destroys the
> relative ordering between the various columns. For example, I would
> like:
>
> [[2,1,3],
>  [3,5,1],
>  [0,3,1]]
>
> to go to:
>
> [[1,2,3],
>  [5,3,1],
>  [3,0,1]]
>
> (swap the first and second columns).  So I want to treat the columns
> as objects and sort them.  I can do this if I convert to a python
> list, but I was hoping to avoid doing that because I ultimately need
> to do element-wise bitwise operations.

there is a thread last august on unique rows which might be useful,
and a thread in Dec 2008 for sorting rows

something like

np.unique1d(c.view([('',c.dtype)]*c.shape[1])).view(c.dtype).reshape(-1,c.shape[1])

maybe it's np.unique with numpy 1.4.

Josef
>
> Thanks!
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list