Hello I am a new user to Python and I am having problem doing what I imagined was a fairly basic task.
I have several (>50) csv files containing daily snow depth data. I would like to iterate through the csv files and calculate monthly means for the snow depth. Data example:
Date,SD
1/1/2000,36
1/2/2000,36
1/3/2000,38
1/4/2000,40
2/1/2000,48
2/2/2000,48
In other words I would like to calculate monthly snow depth averages and write the output to a new csv file. I was able to modify a different example of code for my data, but I am receiving Key Errors for using Date as the key value in my Dictionary.
Any advice?
Code so far:
from __future__ import division
import csv
from collections import defaultdict
def default_factory():
return [0, None, None, 0]
reader = csv.DictReader(open(r'C:\SandBox\VALIDATION\TestTable.csv'))
dates = defaultdict(default_factory)
for row in reader:
sd = int(row["SD"])
dates[row["Dates"]][0] += sd
max = dates[row["Dates"]][1]
dates[row["Dates"]][1] = amount if max is None else amount if amount > max else max
min = dates[row["Date"]][2]
dates[row["Dates"]][2] = amount if min is None else amount if amount < min else min
dates[row["Dates"]][3] += 1
for date in dates:
dates[date][3] = dates[date][0]/dates[date][3]
writer = csv.writer(open(r'C:\SandBox\VALIDATION\TestAvg.csv', 'w', newline = ''))
writer.writerow(["Date", "SD", "max", "min", "mean"])
writer.writerows([date] + dates[date] for date in dates)
EDIT: Just to clarify, I am trying to achieve total monthly mean, i.e. January mean, February mean, etc… not calculate a mean for a single date.
You might want to use a dictionary to make the code a little more readable.