简体   繁体   中英

Python numpy How to sort a ndarray based on a row

how do I sort a ndarray based on one of its rows? example:

ndarray = [['a' 'b' 'c']
           ['2' '7' '6']
           ['3' '0' '1']]

I sort based on the second row and i have:

newsortedndarray = [['a' 'c' 'b']
                    ['2' '6' '7']
                    ['3' '1' '0']]

please help

使用argsort 函数获取“列”的顺序。

ndarray[:,np.argsort(ndarray[1,:])]

或者,您可以np.transpose()您的数组使前一列成为新行。

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