Python: Add a column to numpy 2d array

后端 未结 5 1284
醉酒成梦
醉酒成梦 2021-01-02 04:44

I have a 60000 by 200 numpy array. I want to make it 60000 by 201 by adding a column of 1\'s to the right. (so every row is [prev, 1]) Concatenate with axis = 1 doesn\'t wor

5条回答
  •  情话喂你
    2021-01-02 05:23

    Let me just throw in a very simple example with much smaller size. The principle should be the same.

    a = np.zeros((6,2))
        array([[ 0.,  0.],
               [ 0.,  0.],
               [ 0.,  0.],
               [ 0.,  0.],
               [ 0.,  0.],
               [ 0.,  0.]])
    b = np.ones((6,1))
        array([[ 1.],
               [ 1.],
               [ 1.],
               [ 1.],
               [ 1.],
               [ 1.]])
    
    np.hstack((a,b))
    array([[ 0.,  0.,  1.],
           [ 0.,  0.,  1.],
           [ 0.,  0.,  1.],
           [ 0.,  0.,  1.],
           [ 0.,  0.,  1.],
           [ 0.,  0.,  1.]])
    

提交回复
热议问题