Reputation: 59
I have a directory with a lot of files, structured as n rows x 2 columns
. What I'd like to do is to store the contents of these files in a way that the shape of the final array is (nfiles, nrows, 2). Something similar to
array = numpy.array([[[1,1], [1,1], [1,1]], [1,1], [1,1], [1,1]]])
but how to do it for several files?
I've tried
fnames = glob.glob(/path/to/directory/"*.txt")
final_array = [numpy.genfromtxt(fname) for fname in fnames]
but the final shape is (nfiles,), and numpy.reshape
didn't work.
With
import pandas as pd
df_list = [pd.read_csv(filename, header=None, sep=" ") for filename in fnames]
comb = pd.concat(df_list, ignore_index=True, sort=False)
I can create an array with all file contents (presumably in order) in 2 columns. Is there any way to divide this in parts to reshape (nrows is the same for every file)? Note that I don't want separate arrays for each file, but a single array
Thank you all for the help
Upvotes: 0
Views: 40
Reputation: 11
Use np.stack
or If you're reading in a bunch of files in a loop:
import numpy as np
a = np.zeros(m,n,2)
for i in range(m):
# code here
a[i,:,:] = df_list
# code here
Upvotes: 1