6

I have a 2D numpy array that looks like this

array([[5, 0],
       [3, 1],
       [7, 0],
       [2, 1]])

I'd like to (sub) sort by each column (say right to left) to get this:

array([[5, 0],
       [7, 0],
       [2, 1],
       [3, 1]])

How can I do that in numpy?

3
  • Did you try to change the column index like array[np.argsort(array[:, 0])]? Commented Feb 15, 2021 at 19:16
  • 1
    Does this answer your question? Sorting arrays in NumPy by column Commented Feb 15, 2021 at 19:20
  • Yes, that doesnt keep the sorted second column Commented Feb 15, 2021 at 19:20

2 Answers 2

3

Numpy includes a native function for sub-sorting by columns, lexsort:

idx = np.lexsort((arr[:,0], arr[:,1]))
arr_sorted = arr[idx]

Alternatively, you can use pandas syntax if you're more familiar; this will have some memory/time overhead but should be small for < 1m rows:

arr = [
    [5,  0],
    [3,  1],
    [7,  0],
    [2,  1]
]
df = pd.DataFrame(data=arr).sort_values([1,0])
arr_sorted = df.to_numpy()

output (both):

array([[5, 0],
       [7, 0],
       [2, 1],
       [3, 1]])
Sign up to request clarification or add additional context in comments.

3 Comments

Ah yes, but it is a very large array unfortunately
@FlorisFancypants how large is very large? This should be acceptable up to a few million rows anyway
oh, its like 10000, but the lexsort works too, thanks <3
0

You can use np.lexsort to sort an array on multiple columns:

idx = np.lexsort((a[:,0], a[:,1]))

a[idx]

Output:

array([[5, 0], 
       [7, 0],
       [2, 1],
       [3, 1]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.