-1

I have some folders containing text files that each have 9 numbers per row seperated by a comma. Up until now I loaded the data from a single file into a single 2d array like so:

import numpy as np

fn = "file.txt"
data = np.loadtxt(fn, delimiter = ",")

chans = data.shape[1]
vals = data.shape[0]

I simply loaded the data from a text file into a single 2d array and did some some stuff with it. What I would like to do now is load data from multiple text files into a single 2d array.

So far I tried iterating through multiple files and loading the data into a list, then turning the list into an array, but then I got an array of 2d arrays. Is there any way to achieve what I'm looking for? I only recently started working with Python so I am unaware of certain tricks.

1 Answers1

0

You are looking for np.concatenate. You will want to specify axis=0 to have new rows concatenated at the bottom.

>>> import numpy as np
>>> x = np.arange(30).reshape((6,5))
>>> y = np.arange(30).reshape((6,5))
>>> np.concatenate((x,y),axis=0)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29]])
>>>
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thank you very much. Lets say I load all the data into a list of 2d arrays, is there an already implemented method to concatenate a list of arrays or would I have to manually iterate through and find my own way? – Spooky Ougi May 03 '21 at 23:36
  • Did you read the fine documentation, as I did? `np.concatenate` accepts any sequence of arrays and concatenates them all. – Tim Roberts May 03 '21 at 23:43
  • Aha I see, it's that simple, just pass the list as an argument and all is well. Thank you so much for your time! – Spooky Ougi May 03 '21 at 23:58