In: Computer Science
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:
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.
Compute the number of days each month that received no precipitation. (Regard a “trace” amount of precipitation as zero precipitation.)
Compute the total snowfall for each month. (Again, regard a “trace” amount as no snowfall.)
Find the day that had the greatest difference between the high and low temperature for that day.
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()