I am using a loop to open consecutive files and then a second loop to calculate the average of y at specific row nrs (x). Why is the second loop showing the average only of the last file? I would like to append the average from each file into one new dataframe.
path = '...../'
for file in os.listdir(path):
if file.endswith('.txt'):
with open(os.path.join(path, file)) as f:
df = pd.read_csv(f, sep="\t", header=0,usecols=[0,11])
df.columns = ["x", "y"]
average_PAR=[]
list=[]
for (x, y) in df.iteritems():
average_PAR = sum(y.iloc[49:350]) / len(y.iloc[49:350])
list.append(average_PAR)
print(list)
Thank you!