Question

In: Computer Science

Python to analyze weather data from a file. First, go to this Minnesota Dept. of Natural...

Python to analyze weather data from a file. First, go to this Minnesota Dept. of Natural Resources web page, which displays weather data for Minneapolis on each day of the year 2000. Click the CSV link to download a file containing the data. The file is a “comma-separated values” text file of weather data. The first few lines look like this:

   "Date","Maximum Temperature degrees (F)","Minimum Temperature...
   "2000-01-01","35.0","24.0","T","0.00","0.00"
   "2000-01-02","35.0","29.0","0.04","0.50","0.00"
   "2000-01-03","29.0","24.0","T","T","0.00"

The first line of the file contains column headings, and each of the remaining lines contain weather data for one specific day. These lines contain a date followed by the high temperature, low temperature, precipitation, snowfall, and snow depth recorded on that day. A value of “T” indicates a “trace” amount of precipitation or snowfall, which you can regard as zero.

Write some Python code to load the data from the file into one or more NumPy arrays. Then compute the following:

  1. Compute the average high and low temperatures for each month. For example, the average high temperature for January is the average of the high temperatures for all 31 days in January.

  2. Compute the number of days each month that received no precipitation. (Regard a “trace” amount of precipitation as zero precipitation.)

  3. Compute the total snowfall for each month. (Again, regard a “trace” amount as no snowfall.)

  4. Find the day that had the greatest difference between the high and low temperature for that day.

Solutions

Expert Solution

import pandas as pd

file = '<file path>'

df = pd.read_csv(file)
df['Date'] =  pd.to_datetime(df['Date']) # Changes the date to date time object

#################### AVERAGE LOW AND HIGH OF EACH MONTH #################

per = df.Date.dt.to_period("M")  # Removes date from the date field
g = df.groupby(per)                              # Groups by month
g.mean()                                                 # Gives the average of low and high for each month


######## COMPUTE THE NUMBER OF DAYS EACH MONTH THAT RECEIVED NO PRECIPITATION #########

df2 = df.loc[df['Precipitation (inches)'] == 0.00] # Filter data with 0 percipitation
g = df2.groupby(per)
g.count()

####### COMPUTE THE TOTAL SNOWFALL FOR EACH MONTH #############

df['Snow (inches)'] = df['Snow (inches)'].replace(['T'],0.00) # replaces T with 0.00
df['Snow (inches)'] = pd.to_numeric(df['Snow (inches)'])          # Change data type of Snow column
per = df.Date.dt.to_period("M") 
g = df.groupby(per)
g.sum()


####### FIND THE DAY THAT HAD THE GREATEST DIFFERENCE BETWEEN THE HIGH AND LOW TEMPERATURE FOR THAT MONTH ##########

df['diff'] = df['Maximum Temperature degrees (F)'] - df['Minimum Temperature degrees (F)']
df.resample('M',on='Date')['diff'].max()   

Related Solutions

C ++ Data File Preparation 1. Using the original AL Weather Station Data file find all...
C ++ Data File Preparation 1. Using the original AL Weather Station Data file find all records that have a bad data flag (-9999) for either the PRCP, TMAX or TMIN fields. Produce a new data file (call it Filtered_AL_Weather_Station.txt ) that omits those records with bad data flags. This new file will be used in problem 2. NOTE: The temperatures are given in tenths of a degree Celsius. e.g 83 is 8.3 degrees C.    2. Using the filtered data...
What contribution might the weather data file make to the performance gap?
What contribution might the weather data file make to the performance gap?
Python programming problem! Suppose that there is a json file called data from the desktop. I...
Python programming problem! Suppose that there is a json file called data from the desktop. I want to print the value with the key of "year" and "country". Json file below: { "A":{ "year":11, "grade":A, "country":America}, "B":{ "year":18, "grade":C, "country":England}, "C":{ "year":19, "grade":B, "country":China},} I want a code that I can only replace the name of key from year to grade and the code could be work.
how to export source data to excel file in python?
how to export source data to excel file in python?
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
This is for python coding First of all, you need to know what Natural Numbers are....
This is for python coding First of all, you need to know what Natural Numbers are. They are the positive integers (whole numbers) 1, 2, 3, etc., and zero as well. For this project, there are TWO PORTIONS: PORTION 1: Write a program to find the SUM of the first n natural numbers the user wants to add. The program should first prompt the user for how many natural numbers are to be summed. The use for loop to do...
Python 3 Fix the code. It is not saving the data into the xls file Code:...
Python 3 Fix the code. It is not saving the data into the xls file Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Working product").grid(row=4, sticky="W", pady=20, padx=20) #Working product label tk.Label(window, text="Failed...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Find the data for the problem in the first worksheet named LightbulbLife of the data file....
Find the data for the problem in the first worksheet named LightbulbLife of the data file. It gives the data on the lifetime in hours of a sample of 100 lightbulbs. The company manufacturing these bulbs wants to know whether it can claim that its lightbulbs typically last more than 1000 burning hours. So it did a study. Identify the null and the alternate hypotheses for this study. Can this lightbulb manufacturer claim at a significance level of 5% that...
Python 3 Code does not save properly the data in the excel file. it stores the...
Python 3 Code does not save properly the data in the excel file. it stores the data in the same row over and over # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT