Azam
Azam

Reputation: 147

Merge Data From Different Files By Python

I have many dataset from files to be merged and arranged in one single output file. Here is the example of any two datasets to be merged accordingly.

Data 1 from File 1:
     9.00      2.80     13.08     12.78      0.73
    10.00     -3.44     19.30     18.99      0.14
    12.00      2.60     20.28     20.12      0.39

Data 2 from File 2:
     2.00     -7.73     20.04     18.49      0.62
     5.00     -4.82     17.07     16.38      0.59
     6.00     -2.69     12.55     12.25      0.50
     8.00     -3.85     18.06     17.64      0.94
     9.00     -3.59     16.13     15.73      0.64


Expected output in one file:
 9.00      2.80     13.08     12.78      0.73
10.00     -3.44     19.30     18.99      0.14
12.00      2.60     20.28     20.12      0.39
 2.00     -7.73     20.04     18.49      0.62
 5.00     -4.82     17.07     16.38      0.59
 6.00     -2.69     12.55     12.25      0.50
 8.00     -3.85     18.06     17.64      0.94
 9.00     -3.59     16.13     15.73      0.64

Temporarily the script i used using Python loop for is like this:

import numpy as np
import glob

path='./13-stat-plot-extreme-combine/'
files=glob.glob(path+'13-stat*.dat')

for x in range(len(files)):
    file1=files[x]
    data1=np.loadtxt(file1)

np.savetxt("Combine-Stats.dat",data1,fmt='%9.2f')

The problem is only one dataset is saved on that new file. Question how to use concatenate to such case at different axis dataset?

Upvotes: 0

Views: 37

Answers (1)

John Zwinck
John Zwinck

Reputation: 249133

Like this:

arrays = [np.loadtxt(name) for name in files]
combined = np.concatenate(arrays)

Upvotes: 1

Related Questions