1

I created two 3D array like this

import numpy as np

np.random.seed(1)

prices = np.random.randint(10, 20, size = (2, 2, 2)

#prices ->  [[[19 10]    [[19, 19]
#             [14 17]],   [19, 16]]]


clients = []

for i in range (3):
    if i == 0:
        clients.append(np.random.randint(1, 10, size = (2,2)))
    elif i == 1:
        clients.append(np.random.randint(11, 20, size = (2, 2)))
    elif i == 2:
        clients.append(np.random.randint(21, 30, size = (2, 2)))

z = np.array(clients)

# z -> [[[ 8  3]   [[14, 19]   [[21 26]
#        [ 2  6]]   [16, 15]]   [21 25]]]

and after that I was able to concatenate both arrays like this

concatenate = []

for i in prices:
    for j in z:
        concatenate.append(np.concatenate((i,j), axis = 1))

prices2 = np.array(concatenate)


# prices2 ->[[[19 10 8 3]  [[19 10 14 19]  [[19 10 21 26]  [[19 19 8 3]  [[19 19 14 19]  [[19 19 21 26]
#             [14 17 2 6]]  [14 17 16 15]]  [14 17 21 25]]  [19 16 2 6]]  [19 16 16 15]]  [19 16 21 25]]]

What I want is to sort this 3D array in ascending order using the information in the third column of each 2D array, for example:

# new = [[[19 10 8 3]  [[19 19 8 3]  [[19 10 14 19]  [[19 19 14 19]  [[19 10 21 26]  [[19 19 21 26]
#         [14 17 2 6]]  [19 16 2 6]]  [14 17 16 15]]  [19 16 16 15]]  [14 17 21 25]]  [19 16 21 25]]]

#Here 8 < 14 < 21
#     2 < 16 < 21

I know numpy has a function called np.sort but many examples I see uses 2D arrays instead of 3D arrays, and I don't really know how to do this.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • The examples are quite transferrable, as `sort` has an axis argument. Your example is small enough that you can replace everything before "What I want is to sort ..." with just `prices2 = np.array([[[....` and show the actual initialization value. The whole opening very much distracts from the question. – Mad Physicist Mar 31 '20 at 16:01
  • 1
    Taking the most popular (not the selected) answer, `a[:, :, a[:,:, 2].argsort()]]` or so. – Mad Physicist Mar 31 '20 at 16:16
  • Was gonna add an answer improving also the logic to generate `prices2` :( @mad instead of the current loopy solution – yatu Mar 31 '20 at 16:17
  • @yatu. That part is irrelevant to the current question, and if OP has a question about it I would be happy to answer it as well. I won't reopen *this* question for it though. – Mad Physicist Mar 31 '20 at 16:21
  • 1
    Not sure also the proposed soln works here. I think it's more like `prices2[:,0,2].argsort(0)` @mad – yatu Mar 31 '20 at 16:21
  • Fair enough though @mad – yatu Mar 31 '20 at 16:21
  • @yatu. You're probably right. OP's notation is not exactly standard to begin with, so I'm not sure you can say conclusively without clarification. I'm pretty sure my solution is not correct because I just pulled something out of my a**. – Mad Physicist Mar 31 '20 at 16:23
  • Like I said, fair enough :) @mad – yatu Mar 31 '20 at 16:24
  • Is there a way I can contact you @yatu ? – Carlos Eduardo Corpus Mar 31 '20 at 16:26

0 Answers0