Sorting a 2D numpy array by multiple axes

前端 未结 7 2021
小蘑菇
小蘑菇 2020-11-27 11:23

I have a 2D numpy array of shape (N,2) which is holding N points (x and y coordinates). For example:

array([[3, 2],
       [6, 2],
       [3, 6],
       [3,          


        
相关标签:
7条回答
  • 2020-11-27 12:05

    I was struggling with the same thing and just got help and solved the problem. It works smoothly if your array have column names (structured array) and I think this is a very simple way to sort using the same logic that excel does:

    array_name[array_name[['colname1','colname2']].argsort()]
    

    Note the double-brackets enclosing the sorting criteria. And off course, you can use more than 2 columns as sorting criteria.

    0 讨论(0)
提交回复
热议问题