Reputation: 24151
I want to start with an empty 2D NumPy array, and then add some rows to it. However, so far I have only been able to do this with a 1D array. Here is what I have tried so far:
a = numpy.array([])
a = numpy.append(a, [1, 2])
a = numpy.append(a, [8, 8])
print a
The output I get is:
[1, 2, 8, 8]
Whereas I want the output to be:
[[1, 2], [8, 8]]
How can I do this?
Upvotes: 6
Views: 8115
Reputation: 1470
Try this:
>>> a = numpy.empty((0,2),int)
>>> a = numpy.append(a, [[1, 2]], axis=0)
>>> a = numpy.append(a, [[8, 8]], axis=0)
>>> a
array([[ 1, 2],
[ 8, 8]])
Upvotes: 12
Reputation: 32521
>>> import numpy
>>> numpy.vstack(([1, 2], [8, 8]))
array([[1, 2],
[8, 8]])
Upvotes: 0