SlideShare a Scribd company logo
1 of 12
Computer Science 151
An introduction to the art of computing
Homework 2a solution, 2b info
Rudy Martinez
CS151 Spring 2019
Solution to Homework 2 part A
1. Create a file (homework2.py).
2. Your program should open the data file.
3. Read the file into a list.
4. When you see a missing data point in either TMax or TMin you should delete
the row.This row [1994-04-04,70, ] is missing TMin so remove it from the list or do
not add it to the list.
5. You should print a count of the rows (minus the removed rows with missing
data).
6. Write a comma separated value (CSV) file to disk (homework2.csv)
CS151 Spring 2019
Create a file (homework2.py).
1. Open Spyder and get the imports
# import csv to read the data file
import csv
# import datetime to handle dates
from datetime import datetime
#GLOBAL Variable
DATA = []
CS151 Spring 2019
Your program should open the data file.
● The number one problem was opening a file using Windows
○ Key is to know where the file is located and to have the path to the file.
○ This is where the CS Account on the lab machines makes life easier for everyone.
# Homework 2 part 1 Read in CSV
readFile = open ('Albuquerque1994to2018.csv', 'r')
reader = csv.reader(readFile)
CS151 Spring 2019
Read the file into a list.
● Best way to do this is a for loop
# Counter for rows
line_count = 0
# Loop over the whole data file
for row in reader:
if line_count == 0:
#Eat the header
print(row[0],'t',row[1],'t',row[2])
line_count += 1
else:
# Append the row to the DATA list
DATA.append([datetime.strptime(row[0], '%Y-%m-%d').date(), row[1], row[2]])
line_count += 1
# Homework part1 output number of rows in file
print('Number of Rows:', line_count)
CS151 Spring 2019
Clean Data and Count the rows to write to CSV
● For loop is how we get this done.
# Lets create the file to write to
writeFile = open('/home/rudym/Documents/Homework2.csv', 'w')
writer = csv.writer(writeFile)
# Loop over all the rows in DATA
for row in DATA:
# Now lets find empty TMAX, TMIN values and skip writing them
if row[1] != '' and row[2] != '':
# There is a value in both TMAX AND TMIN so write
writer.writerow([row[0], row[1], row[2]])
line_count_out += 1
CS151 Spring 2019
Stats
Raw Data file has 7935 (including header row) rows.
Your program should have wrote 7867 rows to your CSV file.
CS151 Spring 2019
Homework Part B
1. Create a python file and call it homework2b.py
a. This is the file you will turn in.
b. If you do not want to use your part a file you can use the one I provide.
2. Open homework2.csv (written from part A)
3. Create Dictionary
4. Average TMAX for each year
5. Average TMIN for each year
6. Write Date, TMAX average, TMIN average to Clean_Data.csv
CS151 Spring 2019
Part B Algorithm
1. Open Homework2.csv using a csv reader
2. Create List as global variable
a. DATA=[ ]
3. Read each line in from csv into the dictionary
a. Format the date into a datetime object
b. Cast the TMIN and TMAX into a float
4. Create Dictionary to hold summed data
a. AVGDICT = { }
5. Create variables to hold current year, sumTmin, sumTmax,count
6. For every line in DATA list
a. For each year, add to the tmin sum, and tmax sum
b. When year changes, write date, sumTmax, sumTmin to AVGDICT
7. Open csv writer
a. Write AVGDICT to Avg_Data.csv
CS151 Spring 2019
Average
To calculate the average for temperature sum up each of the values, then divide
by the number of days.
Average = Sum/count
How do we do this?
CS151 Spring 2019
Algorithm for calculating sum by year
currYear = first year in file
sumTmax = 0
sumTmin = 0
count = 0
For every row in DATA
if currYear == row[0].year
sumTmax += row[1]
sumTmin += row[2]
Count +=1
Else
Set currYear to row[0]
Set all variables back to 0
If count > 0:
avgTmax = sumTmax/count
avgTmin = sumTmin/count
Write currYear, avgTmax, avgTmin to avgDict
Write avgDict to csv file Avg_Data.csv
Date TMAX,
TMIN
2018-01-01, 53,
20
2018-01-02, 55,
21
2018-01-03, 52,
25
2018-01-04, 54,
21
2018-01-05, 56,
25
2018-01-06, 59,
41
2018-01-07, 55,
27
2018-01-08, 55,
31
2018-01-09, 60,
35
2018-01-10, 51,
CS151 Spring 2019
Writing Dictionary
Remember Dictionary’s take a key: value pair.
Make your key the year
Make your value a list of Tmax, Tmin
Ex. avgDict = {1994:[55,28]}

More Related Content

Similar to CS 151 homework2a

CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lectureRudy Martinez
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]MomenMostafa
 
CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lectureRudy Martinez
 
Twitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics ProjectTwitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics ProjectSotiris Baratsas
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteachingeteaching
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointersMomenMostafa
 
PART 6: FROM GEO INTO YOUR REPORT
PART 6: FROM GEO INTO YOUR REPORTPART 6: FROM GEO INTO YOUR REPORT
PART 6: FROM GEO INTO YOUR REPORTAndrea Antonello
 
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docxprog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docxstilliegeorgiana
 
C chap02
C chap02C chap02
C chap02Kamran
 
SPL 2 | Algorithms, Pseudo-code, and Flowchart
SPL 2 | Algorithms, Pseudo-code, and FlowchartSPL 2 | Algorithms, Pseudo-code, and Flowchart
SPL 2 | Algorithms, Pseudo-code, and FlowchartMohammad Imam Hossain
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21chinthala Vijaya Kumar
 
#include iostream#includectimeusing namespace std;void.docx
#include iostream#includectimeusing namespace std;void.docx#include iostream#includectimeusing namespace std;void.docx
#include iostream#includectimeusing namespace std;void.docxmayank272369
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...AntareepMajumder
 
R programming slides
R  programming slidesR  programming slides
R programming slidesPankaj Saini
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RRajib Layek
 
To write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdfTo write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdfSANDEEPARIHANT
 

Similar to CS 151 homework2a (20)

CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lecture
 
CS151 Deep copy
CS151 Deep copyCS151 Deep copy
CS151 Deep copy
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
C++
C++C++
C++
 
c++
c++c++
c++
 
CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lecture
 
Twitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics ProjectTwitter Mention Graph - Analytics Project
Twitter Mention Graph - Analytics Project
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
PART 6: FROM GEO INTO YOUR REPORT
PART 6: FROM GEO INTO YOUR REPORTPART 6: FROM GEO INTO YOUR REPORT
PART 6: FROM GEO INTO YOUR REPORT
 
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docxprog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
prog-05.pdfProgramming Assignment #5CSci 430, Spring 2.docx
 
C chap02
C chap02C chap02
C chap02
 
C chap02
C chap02C chap02
C chap02
 
SPL 2 | Algorithms, Pseudo-code, and Flowchart
SPL 2 | Algorithms, Pseudo-code, and FlowchartSPL 2 | Algorithms, Pseudo-code, and Flowchart
SPL 2 | Algorithms, Pseudo-code, and Flowchart
 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
 
#include iostream#includectimeusing namespace std;void.docx
#include iostream#includectimeusing namespace std;void.docx#include iostream#includectimeusing namespace std;void.docx
#include iostream#includectimeusing namespace std;void.docx
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
To write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdfTo write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdf
 

More from Rudy Martinez

More from Rudy Martinez (13)

CS 151Exploration of python
CS 151Exploration of pythonCS 151Exploration of python
CS 151Exploration of python
 
CS 151 Classes lecture 2
CS 151 Classes lecture 2CS 151 Classes lecture 2
CS 151 Classes lecture 2
 
CS 151 Classes lecture
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lecture
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lecture
 
CS 151 Midterm review
CS 151 Midterm reviewCS 151 Midterm review
CS 151 Midterm review
 
CS 151 dictionary objects
CS 151 dictionary objectsCS 151 dictionary objects
CS 151 dictionary objects
 
CS151 FIle Input and Output
CS151 FIle Input and OutputCS151 FIle Input and Output
CS151 FIle Input and Output
 
CS151 Functions lecture
CS151 Functions lectureCS151 Functions lecture
CS151 Functions lecture
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture01
Lecture01Lecture01
Lecture01
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lecture01
Lecture01Lecture01
Lecture01
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 

CS 151 homework2a

  • 1. Computer Science 151 An introduction to the art of computing Homework 2a solution, 2b info Rudy Martinez
  • 2. CS151 Spring 2019 Solution to Homework 2 part A 1. Create a file (homework2.py). 2. Your program should open the data file. 3. Read the file into a list. 4. When you see a missing data point in either TMax or TMin you should delete the row.This row [1994-04-04,70, ] is missing TMin so remove it from the list or do not add it to the list. 5. You should print a count of the rows (minus the removed rows with missing data). 6. Write a comma separated value (CSV) file to disk (homework2.csv)
  • 3. CS151 Spring 2019 Create a file (homework2.py). 1. Open Spyder and get the imports # import csv to read the data file import csv # import datetime to handle dates from datetime import datetime #GLOBAL Variable DATA = []
  • 4. CS151 Spring 2019 Your program should open the data file. ● The number one problem was opening a file using Windows ○ Key is to know where the file is located and to have the path to the file. ○ This is where the CS Account on the lab machines makes life easier for everyone. # Homework 2 part 1 Read in CSV readFile = open ('Albuquerque1994to2018.csv', 'r') reader = csv.reader(readFile)
  • 5. CS151 Spring 2019 Read the file into a list. ● Best way to do this is a for loop # Counter for rows line_count = 0 # Loop over the whole data file for row in reader: if line_count == 0: #Eat the header print(row[0],'t',row[1],'t',row[2]) line_count += 1 else: # Append the row to the DATA list DATA.append([datetime.strptime(row[0], '%Y-%m-%d').date(), row[1], row[2]]) line_count += 1 # Homework part1 output number of rows in file print('Number of Rows:', line_count)
  • 6. CS151 Spring 2019 Clean Data and Count the rows to write to CSV ● For loop is how we get this done. # Lets create the file to write to writeFile = open('/home/rudym/Documents/Homework2.csv', 'w') writer = csv.writer(writeFile) # Loop over all the rows in DATA for row in DATA: # Now lets find empty TMAX, TMIN values and skip writing them if row[1] != '' and row[2] != '': # There is a value in both TMAX AND TMIN so write writer.writerow([row[0], row[1], row[2]]) line_count_out += 1
  • 7. CS151 Spring 2019 Stats Raw Data file has 7935 (including header row) rows. Your program should have wrote 7867 rows to your CSV file.
  • 8. CS151 Spring 2019 Homework Part B 1. Create a python file and call it homework2b.py a. This is the file you will turn in. b. If you do not want to use your part a file you can use the one I provide. 2. Open homework2.csv (written from part A) 3. Create Dictionary 4. Average TMAX for each year 5. Average TMIN for each year 6. Write Date, TMAX average, TMIN average to Clean_Data.csv
  • 9. CS151 Spring 2019 Part B Algorithm 1. Open Homework2.csv using a csv reader 2. Create List as global variable a. DATA=[ ] 3. Read each line in from csv into the dictionary a. Format the date into a datetime object b. Cast the TMIN and TMAX into a float 4. Create Dictionary to hold summed data a. AVGDICT = { } 5. Create variables to hold current year, sumTmin, sumTmax,count 6. For every line in DATA list a. For each year, add to the tmin sum, and tmax sum b. When year changes, write date, sumTmax, sumTmin to AVGDICT 7. Open csv writer a. Write AVGDICT to Avg_Data.csv
  • 10. CS151 Spring 2019 Average To calculate the average for temperature sum up each of the values, then divide by the number of days. Average = Sum/count How do we do this?
  • 11. CS151 Spring 2019 Algorithm for calculating sum by year currYear = first year in file sumTmax = 0 sumTmin = 0 count = 0 For every row in DATA if currYear == row[0].year sumTmax += row[1] sumTmin += row[2] Count +=1 Else Set currYear to row[0] Set all variables back to 0 If count > 0: avgTmax = sumTmax/count avgTmin = sumTmin/count Write currYear, avgTmax, avgTmin to avgDict Write avgDict to csv file Avg_Data.csv Date TMAX, TMIN 2018-01-01, 53, 20 2018-01-02, 55, 21 2018-01-03, 52, 25 2018-01-04, 54, 21 2018-01-05, 56, 25 2018-01-06, 59, 41 2018-01-07, 55, 27 2018-01-08, 55, 31 2018-01-09, 60, 35 2018-01-10, 51,
  • 12. CS151 Spring 2019 Writing Dictionary Remember Dictionary’s take a key: value pair. Make your key the year Make your value a list of Tmax, Tmin Ex. avgDict = {1994:[55,28]}