How to sort a numpy array based on the values in a specific row?

2024/10/15 13:19:04

I was wondering how I would be able to sort a whole array by the values in one of its columns.

I have :

array([5,2,8,2,4])

and:

array([[ 0,  1,  2,  3,  4],[ 5,  6,  7,  8,  9],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19],[20, 21, 22, 23, 24]])

I want to append the first array to the second one like this:

array([[ 0,  1,  2,  3,  4],[ 5,  6,  7,  8,  9],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19],[20, 21, 22, 23, 24],[5,  2,  8,  2,  4]])

And then sort the array by the appended row to get either this:

array([[1,  3,  4,  0,  2],[6,  8,  9,  5,  7],[11, 13, 14, 10, 12],[16, 18, 19, 15, 17],[21, 23, 24, 20, 22],[2,  2,  4,  5,  8]])

or this:

array([[ 2,  1,  3,  4,  0],[ 7,  6,  8,  9,  5],[12, 11, 13, 14, 10],[17, 16, 18, 19, 15],[22, 21, 23, 24, 20],[ 8,  5,  4,  2,  2]])

And then remove the appended column to get:

array([[1,  3,  4,  0,  2],[6,  8,  9,  5,  7],[11, 13, 14, 10, 12],[16, 18, 19, 15, 17],[21, 23, 24, 20, 22]])

or:

array([[ 2,  1,  3,  4,  0],[ 7,  6,  8,  9,  5],[12, 11, 13, 14, 10],[17, 16, 18, 19, 15],[22, 21, 23, 24, 20]])

Is there a code to carry out this procedure. I am very new to python. Thanks a lot!

Answer

You can use numpy.argsort to get a list with the sorted indices of your array. Using that you can then rearrange the columns of the matrix.

import numpy as npc = np.array([5,2,8,2,4])    
a = np.array([[ 0,  1,  2,  3,  4],[ 5,  6,  7,  8,  9],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19],[20, 21, 22, 23, 24]])i = np.argsort(c)
a = a[:,i]
https://en.xdnf.cn/q/69279.html

Related Q&A

python regex match optional square brackets

I have the following strings:1 "R J BRUCE & OTHERS V B J & W L A EDWARDS And Ors CA CA19/02 27 February 2003", 2 "H v DIRECTOR OF PROCEEDINGS [2014] NZHC 1031 [16 May 2014]&…

How to open console in firefox python selenium?

Im trying to open firefox console through Selenium with Python. How can I open firefox console with python selenium? Is it possible to send keys to the driver or something like that?

Can python coverage module conditionally ignore lines in a unit test?

Using nosetests and the coverage module, I would like coverage reports for code to reflect the version being tested. Consider this code:import sys if sys.version_info < (3,3):print(older version of …

Delete Pandas DataFrame row where column value is 0

I already read the answers in this thread but it doesnt answer my exact problem. My DataFrame looks like thisLady in the Water The Night Listener Just My Luck Correlation Claudia Puig …

Pyarrow s3fs partition by timestamp

Is it possible to use a timestamp field in the pyarrow table to partition the s3fs file system by "YYYY/MM/DD/HH" while writing parquet file to s3?

flask run vs. python

Im having difficulty getting my flask app to run by using the "python" method. I have no problems usingexport FLASK_APP=microblog.py flask runbut attempting to usepython microblog.pywill resu…

Pandas-Add missing years in time series data with duplicate years

I have a dataset like this where data for some years are missing .County Year Pop 12 1999 1.1 12 2001 1.2 13 1999 1.0 13 2000 1.1I want something like County Year Pop 12 1999 1.1 12…

Saving zip list to csv in Python

How I can write below zip list to csv file in python?[{date: 2015/01/01 00:00, v: 96.5},{date: 2015/01/01 00:01, v: 97.0},{date: 2015/01/01 00:02, v: 93.75},{date: 2015/01/01 00:03, v: 96.0},{date: 20…

unable to download the pipeline provided by spark-nlp library

i am unable to use the predefined pipeline "recognize_entities_dl" provided by the spark-nlp library i tried installing different versions of pyspark and spark-nlp libraryimport sparknlp from…

Can __setattr__() can be defined in a class with __slots__?

Say I have a class which defines __slots__:class Foo(object):__slots__ = [x]def __init__(self, x=1):self.x = x# will the following work?def __setattr__(self, key, value):if key == x:object.__setattr__…