[Numpy-discussion] Reordering 2 dimensional array by column
Nicole Stoffels
nicole.stoffels at forwind.de
Thu Aug 2 08:43:31 EDT 2012
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
More information about the NumPy-Discussion
mailing list