This question already has an answer here:
这个问题在这里已有答案:
- Sorting a python array/recarray by column 4 answers
- 按列4答案对python数组/重新排列进行排序
I'm trying to convert all my codes to Python. I want to sort an array which has two columns so that the sorting must be based on the 2th column in the ascending order. Then I need to sum the first column data (from first line to, for example, 100th line). I used "Data.sort(axis=1)", but it doesn't work. Does anyone have any idea to solve this problem?
我正在尝试将所有代码转换为Python。我想对一个有两列的数组进行排序,以便排序必须基于升序中的第2列。然后我需要对第一列数据求和(从第一行到第100行)。我使用“Data.sort(axis = 1)”,但它不起作用。有没有人有任何想法来解决这个问题?
2 个解决方案
#1
37
Use .argsort()
it returns an numpy.array
of indices that sort the given numpy.array
. You call it as a function or as a method on your array. For example, suppose you have
使用.argsort()它返回一个numpy.array索引,它们对给定的numpy.array进行排序。您可以将其称为函数或数组中的方法。例如,假设你有
import numpy as np
arr = np.array([[-0.30565392, -0.96605562],
[ 0.85331367, -2.62963495],
[ 0.87839643, -0.28283675],
[ 0.72676698, 0.93213482],
[-0.52007354, 0.27752806],
[-0.08701666, 0.22764316],
[-1.78897817, 0.50737573],
[ 0.62260038, -1.96012161],
[-1.98231706, 0.36523876],
[-1.07587382, -2.3022289 ]])
You can now call .argsort()
on the column you want to sort, and it will give you an array of row indices that sort that particular column which you can pass as an index to your original array.
您现在可以在要排序的列上调用.argsort(),它将为您提供一个行索引数组,这些行索引对您可以作为索引传递给原始数组的特定列进行排序。
>>> arr[arr[:, 1].argsort()]
array([[ 0.85331367, -2.62963495],
[-1.07587382, -2.3022289 ],
[ 0.62260038, -1.96012161],
[-0.30565392, -0.96605562],
[ 0.87839643, -0.28283675],
[-0.08701666, 0.22764316],
[-0.52007354, 0.27752806],
[-1.98231706, 0.36523876],
[-1.78897817, 0.50737573],
[ 0.72676698, 0.93213482]])
You can equivalently use numpy.argsort()
你可以等效地使用numpy.argsort()
>>> arr[np.argsort(arr[:, 1])]
array([[ 0.85331367, -2.62963495],
[-1.07587382, -2.3022289 ],
[ 0.62260038, -1.96012161],
[-0.30565392, -0.96605562],
[ 0.87839643, -0.28283675],
[-0.08701666, 0.22764316],
[-0.52007354, 0.27752806],
[-1.98231706, 0.36523876],
[-1.78897817, 0.50737573],
[ 0.72676698, 0.93213482]])
#2
3
sorted(Data, key=lambda row: row[1])
should do it.
sorted(Data,key = lambda row:row [1])应该这样做。
#1
37
Use .argsort()
it returns an numpy.array
of indices that sort the given numpy.array
. You call it as a function or as a method on your array. For example, suppose you have
使用.argsort()它返回一个numpy.array索引,它们对给定的numpy.array进行排序。您可以将其称为函数或数组中的方法。例如,假设你有
import numpy as np
arr = np.array([[-0.30565392, -0.96605562],
[ 0.85331367, -2.62963495],
[ 0.87839643, -0.28283675],
[ 0.72676698, 0.93213482],
[-0.52007354, 0.27752806],
[-0.08701666, 0.22764316],
[-1.78897817, 0.50737573],
[ 0.62260038, -1.96012161],
[-1.98231706, 0.36523876],
[-1.07587382, -2.3022289 ]])
You can now call .argsort()
on the column you want to sort, and it will give you an array of row indices that sort that particular column which you can pass as an index to your original array.
您现在可以在要排序的列上调用.argsort(),它将为您提供一个行索引数组,这些行索引对您可以作为索引传递给原始数组的特定列进行排序。
>>> arr[arr[:, 1].argsort()]
array([[ 0.85331367, -2.62963495],
[-1.07587382, -2.3022289 ],
[ 0.62260038, -1.96012161],
[-0.30565392, -0.96605562],
[ 0.87839643, -0.28283675],
[-0.08701666, 0.22764316],
[-0.52007354, 0.27752806],
[-1.98231706, 0.36523876],
[-1.78897817, 0.50737573],
[ 0.72676698, 0.93213482]])
You can equivalently use numpy.argsort()
你可以等效地使用numpy.argsort()
>>> arr[np.argsort(arr[:, 1])]
array([[ 0.85331367, -2.62963495],
[-1.07587382, -2.3022289 ],
[ 0.62260038, -1.96012161],
[-0.30565392, -0.96605562],
[ 0.87839643, -0.28283675],
[-0.08701666, 0.22764316],
[-0.52007354, 0.27752806],
[-1.98231706, 0.36523876],
[-1.78897817, 0.50737573],
[ 0.72676698, 0.93213482]])
#2
3
sorted(Data, key=lambda row: row[1])
should do it.
sorted(Data,key = lambda row:row [1])应该这样做。