This time I will bring you numpy operationsTwo-dimensional arraysSort by rows and columns, numpy operates two-dimensional arrays by rows and rowsWhat are the precautionsWhat are the practical cases? Get up and take a look.
How to sort according to a certain row or column in a two-dimensional array? Assume that data is a two-dimensional array of type numpy.array, which can be implemented using the argsort function in numpy. The code example is as follows: data = data[data[:,2].argsort()] #按照第3列对行排序
Note: argsort returns only the sorted rows index and will not change the original array. To sort according to a certain row, you can use the transpose operation.
The code is as follows: data = data.T(data.T[:,2].argsort()).T # 按照第3行对列进行排序
The code is as follows:data = data[:,data[2].argsort()]
Recommended reading:
How to perform automated unit testing with Python UnittestPython operates excel to read and write dataThe above is the detailed content of Numpy operates two-dimensional arrays sorted by rows and columns. For more information, please follow other related articles on the PHP Chinese website!