简体   繁体   中英

Sort columns containing numbers in numpy text array based on multiple columns

How to sort the following matrix:

import numpy as np

A = np.array([['2', '2', '2', 'd'],
              ['1', '1', '3', 'c'],
              ['1', '13', '1', 'a'],
              ['1', '11', '3', 'b']], dtype='<U2')

based on the numbers in the text cells and based on multiple columns? With lists sorting works as follows:

sorted([[2, 2, 2, 'd'],
        [1, 1, 3, 'c'],
        [1, 13, 1, 'a'],
        [1, 11, 3, 'b']], key = lambda k: (k[0],k[2],-k[1]))

Out[1]: [[1, 13, 1, 'a'], [1, 11, 3, 'b'], [1, 1, 3, 'c'], [2, 2, 2, 'd']]

but how could I sort the numpy array A similar to the list above?

First trial was not successful ...

sorted(A, key = lambda k: (k[0],k[2],-k[1]))

Traceback (most recent call last):

File "< ipython-input-... >", line 1, in sorted(A, key = lambda k: (k[0],k[2],-k[1]))

TypeError: bad operand type for unary -: 'numpy.str_'

The error says you can't negate numpy.str_ object sounds logical. But for solving your problem, I suggest you first sort the array with reverse ordering and key k[1] and then sort this result with two other keys.

t = sorted(A, key = lambda k: k[1], reverse=True)
t = sorted(t, key = lambda k: (k[0],k[2]))

You have to convert the sliced values to int :

sorted(A, key = lambda k: (int(k[0]),int(k[2]),-int(k[1])))

[array(['1', '13', '1', 'a'], dtype='<U2'),
 array(['1', '11', '3', 'b'], dtype='<U2'),
 array(['1', '1', '3', 'c'], dtype='<U2'),
 array(['2', '2', '2', 'd'], dtype='<U2')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM