Rearrange columns of numpy 2D array

后端 未结 4 1477
庸人自扰
庸人自扰 2020-12-01 04:55

Is there a way to change the order of the columns in a numpy 2D array to a new and arbitrary order? For example, I have an array

array([[10, 20, 30, 40, 50],         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 05:24

    The easiest way in my opinion is:

    a = np.array([[10, 20, 30, 40, 50],
                  [6,  7,  8,  9,  10]])
    print(a[:, [0, 2, 4, 3, 1]])
    

    the result is:

    [[10 30 50 40 20]
     [6  8  10 9  7 ]]
    

提交回复
热议问题