Ordering matrices by column

Question:

I Have this matrix:

matriz_senha:

[['I' 'N' 'T' 'E' 'R' 'E' 'S' 'T' 'I' 'N' 'G']

['D' 'G' 'F' 'F' 'G' 'D' 'A' 'A' 'D' 'V' 'A']

['A' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ']]

Want to order like this, without changing the position of the following rows in alphabethic order, only the first row:

matriz senha:

[['E' 'E' 'G' 'I' 'I' 'N' 'N' 'R' 'S' 'T' 'T']

 ['F' 'D' 'A' 'D' 'D' 'G' 'V' 'G' 'A' 'F' 'A']

 [' ' ' ' ' ' 'A' ' ' ' ' ' ' ' ' ' ' ' ' ' ']]

Is there any way i can do this with numpy?

Asked By: guerni

||

Answers:

Assuming this input:

array([['I', 'N', 'T', 'E', 'R', 'E', 'S', 'T', 'I', 'N', 'G'],
       ['D', 'G', 'F', 'F', 'G', 'D', 'A', 'A', 'D', 'V', 'A'],
       ['A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']],
      dtype='<U1')

Use indexing and np.argsort:

out = matriz_senha[:, np.argsort(matriz_senha[0])]

Output:

array([['E', 'E', 'G', 'I', 'I', 'N', 'N', 'R', 'S', 'T', 'T'],
       ['F', 'D', 'A', 'D', 'D', 'G', 'V', 'G', 'A', 'F', 'A'],
       [' ', ' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ']],
      dtype='<U1')
Answered By: mozway
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.