How to read several files over a loop as a table (panda) and pick one column from each table and append it together

jazzytortoise :

I have several data files each with two columns. Column 1 has the same data in each file while column two changes with each file. I want to create a matrix or a table such that this data is of the form and then carry on with other functions. Would np.loadtxt be easier/better than pandas? column_1 col_2(file1) col3(file2)...col_n(file-n) 1. 1 3 ... 2. 3 32 3 4 2 4 5 9 5 2 5

For now I have this-

for i in range(0,3):
    file = file_name + '%d' %i+'.dat'
    print(file)
    f=open(file, 'r')
    tble = pd.read_table(f, sep='\s+',skiprows= 15, header=None) 
    time=tble[0]
    inten=tble[1]

but merge, append don't seem to work

    tble['inten'] = pd.Series(inten, index=tble.index)
Serge Ballesta :

I would extract all the data file each in its dataframe and then concat the second columns:

tbls = []
for i in range(0,3):
    file = file_name + '%d' %i+'.dat'
    print(file)
    f=open(file, 'r')
    tble = pd.read_table(f, sep='\s+',skiprows= 15, header=None) 
    tbls.append(tble)
df = pd.concat([tbls[0]] + [tble.iloc[:, 1] for tble in tbls[1:]], axis = 1)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=398401&siteId=1