How to add a new row to an empty numpy array How to add a new row to an empty numpy array numpy numpy

How to add a new row to an empty numpy array


The way to "start" the array that you want is:

arr = np.empty((0,3), int)

Which is an empty array but it has the proper dimensionality.

>>> arrarray([], shape=(0, 3), dtype=int64)

Then be sure to append along axis 0:

arr = np.append(arr, np.array([[1,2,3]]), axis=0)arr = np.append(arr, np.array([[4,5,6]]), axis=0)

But, @jonrsharpe is right. In fact, if you're going to be appending in a loop, it would be much faster to append to a list as in your first example, then convert to a numpy array at the end, since you're really not using numpy as intended during the loop:

In [210]: %%timeit   .....: l = []   .....: for i in xrange(1000):   .....:     l.append([3*i+1,3*i+2,3*i+3])   .....: l = np.asarray(l)   .....: 1000 loops, best of 3: 1.18 ms per loopIn [211]: %%timeit   .....: a = np.empty((0,3), int)   .....: for i in xrange(1000):   .....:     a = np.append(a, 3*i+np.array([[1,2,3]]), 0)   .....: 100 loops, best of 3: 18.5 ms per loopIn [214]: np.allclose(a, l)Out[214]: True

The numpythonic way to do it depends on your application, but it would be more like:

In [220]: timeit n = np.arange(1,3001).reshape(1000,3)100000 loops, best of 3: 5.93 µs per loopIn [221]: np.allclose(a, n)Out[221]: True


Here is my solution:

arr = []arr.append([1,2,3])arr.append([4,5,6])np_arr = np.array(arr)


In this case you might want to use the functions np.hstack and np.vstack

arr = np.array([])arr = np.hstack((arr, np.array([1,2,3])))# arr is now [1,2,3]arr = np.vstack((arr, np.array([4,5,6])))# arr is now [[1,2,3],[4,5,6]]

You also can use the np.concatenate function.

Cheers