我正在读取具有3行3列的.csv。我将latitude
和longitude
发送到Web服务以获取24小时的天气信息。编辑:这是一个样本量,生产数据将有1000行。
我的输入数据的结构是:
field id| latitude| longitude
345 44.79721 -95.1756
789 44.7963 -95.1809
012 44.7954 -95.1925
我每行发送3个请求,以接收hourly temperature
,hourly relative humidity
和“每小时降水量”
我的输出在24小时内应该是这样的。数据表示来自startHour
的值,这表示下面的第一行,尽管我正在00:00:00到01:00:00调用API。我想将开始时间和日期作为我的时间值保留在输出中。
time | field id | Temp | Humidity | Precip
2019-05-06 00:00:00 345 65.5 7.8 0.0
2019-05-06 01:00:00 345 62.5 5.7 0.1
2019-05-06 02:00:00 345 59.2 3.1 0.0
2019-05-06 00:00:00 789 55.5 7.5 0.0
2019-05-06 01:00:00 789 59.5 5.2 0.0
2019-05-06 02:00:00 789 53.2 3.8 0.0
2019-05-06 00:00:00 012 58.5 7.5 0.0
2019-05-06 01:00:00 012 54.3 7.7 0.0
2019-05-06 02:00:00 012 53.2 10.8 0.2
我应该在代码中进行哪些更改,以写入上面示例中指定的日期和数据结构?请原谅格式或繁琐的代码。这是我的输出示例:
这是我的代码。
import pandas as pd
import requests
import json
import config
from datetime import datetime, timedelta
from pandas import DataFrame
df = pd.read_csv(config.DATA_PATH / config.input_csv,
usecols=['field id', 'lat', 'lon'],
dtype=str)
for _, id_, lat, lon in df.itertuples():
rng = pd.date_range(pd.Timestamp(config.dayVal + ' ' + "'06:00:00'"),
periods=24, freq='H')
dtSeries = pd.Series(rng.format())
ddf = dtSeries.to_frame(name='Date')
ddf['time'] = pd.to_datetime(ddf['Date'])
dateDF = ddf['dates'] = ddf['time'].dt.date
timeDF = ddf['dates'] = ddf['time'].dt.time
startDayVal= dateDF[::2]
endDayVal = dateDF[1::2]
startTimeVal= timeDF[::2]
endTimeVal = timeDF[1::2]
startDayValStr = (startDayVal.to_string(index=False)).replace(' ','').split('\n')
endDayValStr =(endDayVal.to_string(index=False)).replace(' ','').split('\n')
startTimeValStr = (startTimeVal.to_string(index=False)).replace(' ','').split('\n')
endTimeValStr = (endTimeVal.to_string(index=False)).replace(' ','').split('\n')
timeStr = (timeDF.to_string(index=False)).replace(' ','').split('\n')
for _, id_, lat, lon in df.itertuples():
for t in timeStr:
for startDate, endDate, startTime, endTime, t in zip (startDayValStr, endDayValStr, startTimeValStr, endTimeValStr, timeStr):
time_param = '?start='+ startDate +'T'+startTime + 'Z' + '&end='+ endDate + 'T' + endTime + 'Z'
hrPrecip = 'https://insight.api.wdtinc.com/hourly-precipitation/' + str(lat)+'/' + str(lon) + time_param + '&unit=inches'
hrTemp = 'https://insight.api.wdtinc.com/hourly-temperature/' + str(lat)+'/' + str(lon) + time_param + '&unit=fahrenheit'
hrHumidity = 'https://insight.api.wdtinc.com/hourly-relative-humidity/' + str(lat)+'/' + str(lon) + time_param
resp_pre_hr = se_.get(hrPrecip, auth=(config.key, config.password), timeout=30)
resp_temp_hr = se_.get(hrTemp, auth=(config.key, config.password), timeout=60)
resp_rh_hr = se_.get(hrHumidity, auth=(config.key, config.password), timeout=90)
preValHrly = str(json.loads(resp_pre_hr.content)['series'][0]['value'])
tempValHrly = str(json.loads(resp_temp_hr.content)['series'][0]['value'])
humidityHrly = str(json.loads(resp_rh_hr.content)['series'][0]['value'])
hrly_df = pd.DataFrame({'dateTime' :[t], 'field id': [id_], 'HourlyPrecipIn': [preValHrly],'HourlyRH' : [humidityHrly], 'HourlyTempF' : [tempValHrly]})
hrly_df = hrly_df[['dateTime','field id','HourlyPrecipIn','HourlyRH', 'HourlyTempF']]
hrly_df.head()
hrlydfs = hrLylst.append(hrly_df)
pd.concat(hrLylst).to_csv(config.output_csv_hourly, encoding='utf-8', index=False)