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?

2
  • Why are you using -0 instead of just 0? Commented May 29, 2015 at 12:06
  • Very new to python, found it on the web and am copy pasting. I don't really know what the axis bit does anyway... Commented May 29, 2015 at 12:09

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.