Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:41:43+00:00 2026-06-01T02:41:43+00:00

Hello I am a new user to Python and I am having problem doing

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:41:44+00:00Added an answer on June 1, 2026 at 2:41 am

    You might want to use a dictionary to make the code a little more readable.

    from __future__ import division
    import csv
    from collections  import defaultdict
    
    def default_factory():
       return { "sum": 0, "max": None, "min": None, "count": 0}
    
    reader = csv.DictReader(open(r'sd.csv'))
    
    dates = defaultdict(default_factory)
    rows = []
    for row in reader:
        date = row["Date"]
        sd = int(row["Snowdepth"])
        rows.append([date, sd])
        month = date.split("/")[0]
        r = dates[month]
        r["sum"] += sd
        max = r["max"]
        r["max"] = sd if max is None else sd if sd > max else max
        min = r["min"]
        r["min"] = sd if min is None else sd if sd < min else min
        r["count"] += 1
    
    for date in dates:
        r = dates[date]
        r["avg"] = r["sum"]/r["count"]
    
    writer = csv.writer(open(r'TestAvg.csv', 'w'))
    writer.writerow(["Date", "SD", "max", "min", "mean"])
    for row in rows:
        r = dates[row[0].split("/")[0]]
        writer.writerow(row + [r["max"], r["min"], r["avg"]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a user model and a ratings model. Whenever a new user
Possible Duplicate: New Cool Features of C# 4.0 Hello, There are several(many) questions at
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
In my relationships_controller I have the following: class RelationshipsController < ApplicationController def new @user
Hello and thank you for reading, I have a task to authenticate a un
I am having some trouble as follows: When i add a new user, it
class Hello @hello = hello def display puts @hello end end h = Hello.new
hello I'm new to PHP programming and I migrated from ASP .net to PHP..
Hello I'm new to ontologies, my assignment is to create an ontology using Protege
C:\>irb irb(main):001:0> s = Proc.new { puts Hello } => #<Proc:0x04051780@(irb):1> irb(main):002:0> s.call Hello

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.