[Numpy-discussion] Reordering 2 dimensional array by column
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]]) 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! Best regards, Nicole ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Reordering 2 dimensional array by column
Hi, On Thu, Aug 2, 2012 at 3:43 PM, Nicole Stoffels nicole.stoff...@forwind.dewrote: 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 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Reordering 2 dimensional array by column
Thanks eat! It helps and it's so easy! :) On 02.08.2012 14:59, eat wrote: Hi, On Thu, Aug 2, 2012 at 3:43 PM, Nicole Stoffels nicole.stoff...@forwind.de mailto:nicole.stoff...@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 mailto:NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion