Right now I have a 2x2 ND-array, namely np.array([[93, 95], [84, 100], [99, 87]])
. I would like to reverse the second column of the array into, such that I get: np.array([[93, 87], [84, 100], [99, 95]])
.
I tried the following code:
grades = np.array([[93, 95], [84, 100], [99, 87]])
print(grades[::-1,:])
However, the result I get is
[[ 99 87]
[ 84 100]
[ 93 95]]
I understand that this is because I am reversing all of the entries in the 1-axis, which is why the entries in the first column is also reversed. So what code can I write to get:
[[ 93 87]
[ 84 100]
[ 99 95]]
Use numpy.flip
function to reverse the order of values in specific column(s), though it's more suitable for a more extended/general cases:
grades = np.array([[93, 95], [84, 100], [99, 87], [99, 83]])
grades[:, -1] = np.flip(grades[:, -1])
print(grades)
Or just use the reversed order of rows:
grades[:, -1] = grades[::-1, -1]
[[ 93 83]
[ 84 87]
[ 99 100]
[ 99 95]]