In Python 3 I am importing several data files in a loop, and I would like to be able to store all the data in a single 2-dimensional array. I start with something like data = np.array([]) and on each iteration i want to add a new array datai = np.array([1,2,3]), how can I get my final array to look like this? [[1,2,3],[1,2,3],...,[1,2,3]]
I have tried np.append, np.concatenate, and np.stack, but none seem to work. An example code that I'm trying:
data = np.array([])
for i in range(datalen):
datai = *func to load data as array*
data = np.append(data, datai)
but of course this returns a flattened array. Is there any way I can get back a 2-dimensional array of length datalen with each element being the array datai?
Thanks!
vstack. Are all of your subarrays the same length?np.appenddocs. What does it say about omitting theaxisparameter?np.empty([])is not the same as the list[]. Look at itsshapeandndim. Forconcatenateto work, the inputs have to have matching dimensions.data1is 1d (3,). It can be joined on axis 0 to another 1d array. To join on a new initial axis it needs to be 2d (which is whatvstackaddresses). In any case repeated list append is the right way, not a repeated concatenate.data1? How do you load it? Often, when loaded from a txt file, the resulting array will be 2d. It could of course be 1d.