The following makes use of the glob
module to get a list of all files in the current folder of the form X*.csv
, i.e. all CSV files starting with x
. For each file it finds, it first skips a header row (optional) and it then loads all remaining rows using a zip()
trick to transpose the list of rows into a list of columns.
For each column, it converts each cell into an integer and sums the values, dividing this total by the number of elements found, thus giving an average for each column. It then writes the values to your output result.csv
in the format filename, av_col1, av_col2
etc:
import glob
import csv
with open('result.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output)
for filename in glob.glob('X*.csv'):
print (filename)
with open(filename, newline='') as f_input:
csv_input = csv.reader(f_input)
header = next(csv_input)
averages = []
for col in zip(*csv_input):
averages.append(sum(int(x) for x in col) / len(col))
csv_output.writerow([filename] + averages)
So if you had XY0001.csv
containing:
Col1,Col2,Col3
6,1,10
2,1,20
5,2,30
result.csv
would be written as follows:
XY0001.csv,4.333333333333333,1.3333333333333333,20.0
Tested using Python 3.5.2