I know that we can sort the columns of an 2D numpy array based on a row the following way:
a = np.array([[1,4,7],
[3,1,5],
[9,5,8]])
a = a[:, a[1, :].argsort()]
Out: [[4,1,7],
[1,3,5],
[5,9,8]]
Please note that this is indeed what I want. The second row (index=1) is now sorted and the values in rows 0 and 2 also shifted accordingly. That is, the column positions change based on the sorting order of row 1.
But now to my problem: I don't have a 2D array but a 3D array (i.e. an array of 2D arrays).
a = np.array([[[1,4,7],
[3,1,5],
[9,5,8]],
[[2,8,7],
[3,8,1],
[9,2,8]]])
I still want to sort the columns of the 2D arrays, individually, based on the values of their respective rows 1. The desired result would be:
([[[4,1,7],
[1,3,5],
[5,9,8]],
[[7,2,8],
[1,3,8],
[8,9,2]]])
I tried the following but the results are not as desired:
a = a[:, :, a[: , 1, :].argsort()]