Appending arrays from different files and insert a column to data

Viewed 31

The data in my case are 2D arrays available in multiple files. In the end, I would like to append these 2D arrays row-wise. For example, say fileA contains

1 2 3
4 5 6

and fileB contains

7 8 9
0 1 2

I would like the resulting operation to be

1 2 3
4 5 6
7 8 9
0 1 2

The easiest way to do this is as per the solution here something like

data = np.concatenate([np.loadtxt(f) for f in filenames])

However, I would like to include a column during this operation. For example, let x = [3, 5] with length of x = number of files. Each file's row length is fixed (and in this case is 2). I would like the resulting array to be:

3 1 2 3
3 4 5 6
5 7 8 9
5 0 1 2

How to achieve this in a pythonic way?

1 Answers

Try with pd.concat:

pd.concat([pd.read_csv(f, header=None).assign(x=x) 
           for (f,x) in zip(filenames, x_list)]

You would get the x column last. If you insist on having it first, you can try:

pd.concat([pd.read_csv(f, header=None) for f in filenames],
          keys=x_list).reset_index(level=0)
Related