Since I am learning python please can someone give me some advices or which python package to use to improve my code:
This is my code:
import pandas as pd
import numpy as np
source = pd.read_table("data1.dat", delim_whitespace=True,header=None)
v = source.iloc[:,0]
f = source.iloc[:,1]
w1 = np.average(f[(v>40) & (v<42)])
w2 = np.average(f[(v>44) & (v<45)])
w3 = np.average(f[(v>50) & (v<52)])
I need to run this code on 4 datasets (they have the same shape).
How I can loop (or maybe loop is not needed) through my folder and run my code on data1.dat, data2.dat, data3.dat and data4.dat.
After this is done, I would like to save those results into one table which looks like this:
d1 d2 d3 d4
w1 w1 w1 w1
w2 w2 w2 w2
w3 w3 w3 w3
(d1 d2 d3 d4 is header or name of each column).
Thanks.
CodePudding user response:
Let's say the files are on your Desktop
path = r'C:\Users\user\Desktop'
Iterate over all files in the path
import os
import glob
import pandas as pd
import numpy as np
for file in glob.glob(os.path.join(path, '*.dat')):
source = pd.read_table(file, delim_whitespace=True,header=None)
v = source.iloc[:,0]
f = source.iloc[:,1]
w1 = np.average(f[(v>40) & (v<42)])
w2 = np.average(f[(v>44) & (v<45)])
w3 = np.average(f[(v>50) & (v<52)])