-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherPy_API_Challenge_Billy_Zhao.py
More file actions
233 lines (156 loc) · 5.61 KB
/
WeatherPy_API_Challenge_Billy_Zhao.py
File metadata and controls
233 lines (156 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
# coding: utf-8
# # WeatherPy
# ----
#
# ### Analysis
# * As expected, the weather becomes significantly warmer as one approaches the equator (0 Deg. Latitude). More interestingly, however, is the fact that the southern hemisphere tends to be warmer this time of year than the northern hemisphere. This may be due to the tilt of the earth.
# * There is no strong relationship between latitude and cloudiness. However, it is interesting to see that a strong band of cities sits at 0, 80, and 100% cloudiness.
# * There is no strong relationship between latitude and wind speed. However, in northern hemispheres there is a flurry of cities with over 20 mph of wind.
#
# ---
#
# #### Note
# * Instructions have been included for each segment. You do not have to follow them exactly, but they are included to help you think through the steps.
# In[1]:
# Dependencies and Setup
import pandas as pd
import numpy as np
import requests
import datetime
import matplotlib.pyplot as plt
from pandas.io.json import json_normalize
import seaborn as sns; sns.set() # trying a new package called seaborn to generate plots
# Import API key
from config import api_key
# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy
# Output File (CSV)
output_data_file = "output_data/cities.csv"
# Range of latitudes and longitudes
lat_range = (-90, 90)
lng_range = (-180, 180)
# Today's time
now = datetime.datetime.now()
todayDate = now.strftime("%m/%d/%Y")
# ## Generate Cities List
# In[2]:
# List for holding lat_lngs and cities
lat_lngs = []
cities = []
# Create a set of random lat and lng combinations
lats = np.random.uniform(low=-90.000, high=90.000, size=1500)
lngs = np.random.uniform(low=-180.000, high=180.000, size=1500)
lat_lngs = zip(lats, lngs)
# Identify nearest city for each lat, lng combination
for lat_lng in lat_lngs:
city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name
# If the city is unique, then add it to a our cities list
if city not in cities:
cities.append(city)
# Print the city count to confirm sufficient count
len(cities)
# ### Perform API Calls
# * Perform a weather check on each city using a series of successive API calls.
# * Include a print log of each city as it'sbeing processed (with the city number and city name).
#
# In[3]:
# base URL for open weather API
baseURL = "http://api.openweathermap.org/data/2.5/weather?"
# df is the base dataframe with api responses
# I use a json_normalize() function to flatten and normalize json responses into dataframe
df = pd.DataFrame()
x = 1
print('Beginning Data Retrieval')
print('-' * 38)
for city in cities:
queryUrl = baseURL + 'appid=' + api_key + '&q=' + city + '&units=imperial'
try:
response = requests.get(queryUrl).json()
if response['cod'] == 200:
print(f"Processing Record data for {x} of Set 1 | {city}")
df = df.append(json_normalize(response), sort=True)
x = x+1
else:
x = x-1
pass
except Exception as e:
x = x-1
pass
# In[4]:
## check api response data, occasionally, you get humidity reading above 100%
## these are bad data that may skew the plot, so they need to be dropped
df.loc[df['main.humidity'] > 100]
# In[5]:
# show all the column heads of the raw dataframe
list(df)
# In[6]:
# take a look at the columns and raw dataset - there are some odd columns and missing values
df.count()
# In[7]:
# drop humidity > 100% rows, check data integrity
df = df.rename(columns={'main.humidity' : 'humidity'})
df = df[df.humidity <= 100]
df.count()
# ### Convert Raw Data to DataFrame
# * Export the city data into a .csv.
# * Display the DataFrame
# In[8]:
# df1 is modified dataframe with relevent data for final reports
df1 = pd.DataFrame()
df1['City'] = df['name']
df1['Cloudiness'] = df['clouds.all']
df1['Country'] = df['sys.country']
df1['Humidity'] = df['humidity']
df1['Date'] = df['dt']
df1['Latitude'] = df['coord.lat']
df1['Longitude'] = df['coord.lon']
df1['Max Temp'] = df['main.temp_max']
df1['Wind Speed'] = df['wind.speed']
# export results to output csv file
df1.to_csv(output_data_file)
# display dataframe
df1.head()
# In[9]:
# check data integrity - should have same number each columns
df1.count()
# ### Plotting the Data
# * Use proper labeling of the plots using plot titles (including date of analysis) and axes labels.
# * Save the plotted figures as .pngs.
# #### Latitude vs. Temperature Plot
# In[10]:
# plotting Lat vs Temp using
x = df1['Latitude']
y = df1['Max Temp']
tempPlot = sns.set_style("whitegrid")
tempPlot = sns.scatterplot(x, y, data=df1)
tempPlot.set(xlabel='Latitude', ylabel='Max Tempreture (F)')
plt.title(f'City Latitude vs Max Temperature ({todayDate})')
plt.show()
# #### Latitude vs. Humidity Plot
# In[11]:
x = df1['Latitude']
y = df1['Humidity']
tempPlot = sns.set_style("whitegrid")
plt.title(f'City Latitude ({todayDate})')
tempPlot = sns.scatterplot(x, y, data=df1,)
tempPlot.set(xlabel='Latitude', ylabel='Humidity (%)')
plt.show()
# #### Latitude vs. Cloudiness Plot
# In[12]:
x = df1['Latitude']
y = df1['Cloudiness']
tempPlot = sns.set_style("whitegrid")
tempPlot = sns.scatterplot(x, y, data=df1)
tempPlot.set(xlabel='Latitude', ylabel='Cloudiness (%)')
plt.title(f'City Latitude vs Cloudiness ({todayDate})')
plt.show()
# #### Latitude vs. Wind Speed Plot
# In[13]:
x = df1['Latitude']
y = df1['Wind Speed']
tempPlot = sns.set_style("whitegrid")
tempPlot = sns.scatterplot(x, y, data=df1)
tempPlot.set(xlabel='Latitude', ylabel='Wind Speed (mph)')
plt.title(f'Latitude vs Wind Speed ({todayDate})')
plt.show()