May-14-2018, 06:13 PM
Hey guys-I'm working on a script that grabs weather information from different locations. The only thing I'm missing within the dataframe is replacing "0_x" and "0_y" (below) with a reference to the location.
Ref 0_x 0_y
0 temp 83 71
1 humid 19% 45%
2 pressure 29 29
3 dew 37 49
4 wind_dir 157 227
5 wind_mph 6.5 1.2
6 wind_gust 9.8 3.7
Within the csv file that holds the location info, there is a 3rd column with specific location names. All I need to add is a line in the script that references the 3rd column and inserts it as a column header. Hopefully this all makes sense. Any help would be greatly appreciated.
Ref 0_x 0_y
0 temp 83 71
1 humid 19% 45%
2 pressure 29 29
3 dew 37 49
4 wind_dir 157 227
5 wind_mph 6.5 1.2
6 wind_gust 9.8 3.7
Within the csv file that holds the location info, there is a 3rd column with specific location names. All I need to add is a line in the script that references the 3rd column and inserts it as a column header. Hopefully this all makes sense. Any help would be greatly appreciated.
import requests import csv import pandas as pd stadiums = pd.read_csv('ParkLocations1.csv') stadiums.to_dict('series') all_loc_data = pd.DataFrame() col_1 = ['temp','humid','pressure','dew','wind_dir','wind_mph','wind_gust'] all_loc_data.insert(loc=0, column='Ref', value=col_1) url_template = "http://api.wunderground.com/api/88fb15b49e765a43/conditions/q/{state}/{city}.json" for state,city in zip(stadiums.loc[ : ,"State"],stadiums.loc[ : ,"City"]): url = requests.get(url_template.format(state=state,city=city)) parsed = url.json() temp = parsed['current_observation']['temp_f'] humid = parsed['current_observation']['relative_humidity'] pressure = parsed['current_observation']['pressure_in'] dew = parsed['current_observation']['dewpoint_f'] wind_dir = parsed['current_observation']['wind_degrees'] wind_mph = parsed['current_observation']['wind_mph'] wind_gust = parsed['current_observation']['wind_gust_mph'] data_list = [temp,humid,pressure,dew,wind_dir,wind_mph,wind_gust] loc_data = pd.DataFrame(data_list) loc_data.insert(loc=0, column='Ref', value=col_1) all_loc_data = all_loc_data.merge(loc_data, on='Ref',how='outer') all_loc_data.to_csv('WUEx.csv')