1

Very new to Python so please bear with me here. I am trying to sort an array that I have imported into python with numpy.sort:

 guy = numpy.sort(sasBody, axis=-0)

The first column is a column of strings, so I would like to alphabetically sort the array. The problem I am having is that it does sort the first column, however all the numbers associated with the prior rows are now not connected to its correct first column counterpart.

What am I doing wrong?

Lererferler
  • 287
  • 4
  • 19

2 Answers2

3

You need to use np.lexsort though in case of strings that may not work. As a work around, you may use np.argsort

>>> a
array([['xyz', 0],
       ['abc', 5],
       ['ijk', 10]], dtype=object)
>>> i = np.argsort(a[:,0])
>>> a[i]
array([['abc', 5],
       ['ijk', 10],
       ['xyz', 0]], dtype=object)
behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
1

Sorry a little bit more digging found me the answer guy = sasBody[np.argsort(sasBody[:, 0])]

Lererferler
  • 287
  • 4
  • 19