Python Numpy排序行 [英] Python Numpy Sort rows

查看:47
本文介绍了Python Numpy排序行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Python来说是新手,所以请在这里与我联系.我正在尝试使用numpy.sort对导入到python中的数组进行排序:

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.

我在做什么错了?

推荐答案

您需要使用 np.lexsort ,即使在字符串可能不起作用的情况下.解决方法是,您可以使用 np.argsort

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)

这篇关于Python Numpy排序行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆