[Numpy-discussion] Reordering 2 dimensional array by column
eat
e.antero.tammi@gmail....
Thu Aug 2 07:59:12 CDT 2012
Hi,
On Thu, Aug 2, 2012 at 3:43 PM, Nicole Stoffels
<nicole.stoffels@forwind.de>wrote:
> Dear all,
>
> I have a two-dimensional array:
>
> a = array([[1,2,3],[0,2,1],[5,7,8]])
>
> I want to reorder it by the last column in descending order, so that I get:
>
> b =array([[5, 7, 8],[1, 2, 3],[0, 2, 1]])
>
Perhaps along the lines:
In []: a
Out[]:
array([[1, 2, 3],
[0, 2, 1],
[5, 7, 8]])
In []: ndx= a[:, 2].argsort()
In []: a[ndx[::-1], :]
Out[]:
array([[5, 7, 8],
[1, 2, 3],
[0, 2, 1]])
>
> What I did first is the following, which reorders the array in ascending
> order (I found that method in the internet):
>
> b = array(sorted(a, key=lambda new_entry: new_entry[2]))
> b = array([[0, 2, 1],[1, 2, 3],[5, 7, 8]])
>
> But I want it just the other way arround. So I did the following
> afterwards which results in an array only containing zeros:
> b_indices = b.argsort()
> b_matrix = b[b_indices[::-1]]
> new_b = b_matrix[len(b_matrix)-1]
>
> Is there an easy way to reorder it? Or is there at least a complicated
> way which produces the right output?
>
> I hope you can help me! Thanks!
>
My 2 cents,
-eat
>
> Best regards,
>
> Nicole
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120802/70e97d38/attachment.html
More information about the NumPy-Discussion
mailing list