Numpy - add row to array

后端 未结 10 1367
难免孤独
难免孤独 2020-11-27 10:59

How does one add rows to a numpy array?

I have an array A:

A = array([[0, 1, 2], [0, 2, 0]])

I wish to add rows to this array from

10条回答
  •  悲哀的现实
    2020-11-27 11:38

    I use numpy.insert(arr, i, the_object_to_be_added, axis) in order to insert object_to_be_added at the i'th row(axis=0) or column(axis=1)

    import numpy as np
    
    a = np.array([[1, 2, 3], [5, 4, 6]])
    # array([[1, 2, 3],
    #        [5, 4, 6]])
    
    np.insert(a, 1, [55, 66], axis=1)
    # array([[ 1, 55,  2,  3],
    #        [ 5, 66,  4,  6]])
    
    np.insert(a, 2, [50, 60, 70], axis=0)
    # array([[ 1,  2,  3],
    #        [ 5,  4,  6],
    #        [50, 60, 70]])
    

    Too old discussion, but I hope it helps someone.

提交回复
热议问题