How to add column to numpy array

前端 未结 4 2072
北海茫月
北海茫月 2020-12-02 14:09

I am trying to add one column to the array created from recfromcsv. In this case it\'s an array: [210,8] (rows, cols).

I want to add a nint

4条回答
  •  不知归路
    2020-12-02 14:56

    It can be done like this:

    import numpy as np
    
    # create a random matrix:
    A = np.random.normal(size=(5,2))
    
    # add a column of zeros to it:
    print(np.hstack((A,np.zeros((A.shape[0],1)))))
    

    In general, if A is an m*n matrix, and you need to add a column, you have to create an n*1 matrix of zeros, then use "hstack" to add the matrix of zeros to the right of the matrix A.

提交回复
热议问题