LoginSignup
4
3

More than 5 years have passed since last update.

Python Numpy array で列(Column) を指定してソートする方法。

Posted at
Python初心者のメモ。内容の間違いなどご注意ください。
import numpy as np

def practice():
    data = np.array([[10,9],[8,7],[6,5],[4,3],[2,1]])

    print "Before:"
    print data 
    print 

    # argsort()で最後の列をソートして順番に並べ替える
    # 必要に応じて'-1'を指定する列に変える
    data = data[data[:,-1].argsort()]

    print "After:" 
    print data

if __name__ == '__main__':
    practice()
    pass
Output:

Before:
[[10 9]
[ 8 7]
[ 6 5]
[ 4 3]
[ 2 1]]

After:
[[ 2 1]
[ 4 3]
[ 6 5]
[ 8 7]
[10 9]]

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3