Q:

Sort following NumPy array using python programming

belongs to collection: Python NumPy Exercises

0

Sort following NumPy array

  • Case 1: Sort array by the second row
  • Case 2: Sort the array by the second column
sampleArray = numpy.array([[34,43,73],[82,22,12],[53,94,66]])

Expected Output:

Printing Original array
[[34 43 73]
 [82 22 12]
 [53 94 66]]

Sorting Original array by second row
[[73 43 34]
 [12 22 82]
 [66 94 53]]

Sorting Original array by second column
[[82 22 12]
 [34 43 73]
 [53 94 66]]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution:

import numpy

print("Printing Original array")
sampleArray = numpy.array([[34,43,73],[82,22,12],[53,94,66]]) 
print (sampleArray)

sortArrayByRow = sampleArray[:,sampleArray[1,:].argsort()]
print("Sorting Original array by secoond row")
print(sortArrayByRow)

print("Sorting Original array by secoond column")
sortArrayByColumn = sampleArray[sampleArray[:,1].argsort()]
print(sortArrayByColumn)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Print max from axis 0 and min from axis 1 from the... >>
<< Split the array into four equal-sized sub-arrays u...