I would like to write these data frames to a csv
file, however I am only writing the last row. The objective of my code is to call an API and get the highest temperature for "yesterday" for a given lat/lng.
My current output is:
I would like for my output to be:
Code:
import pandas as pd
import requests
import csv
import json
from datetime import datetime, timedelta
from pandas import DataFrame
#read csv
output_high_csv = r"C:\wab\outputempbeets1.csv"
se_ = requests.Session()
df = pd.read_csv(csvdata)
df = df [['lat', 'lon', 'field id']]
#convert Pandas series to string
dfText = df.astype(basestring)
#get lat/lng from dataframe
dfLat = dfText['lat']
dfLon = dfText['lon']
dfFid = dfText['field id']
#get values from series
latval = list(dfLat.values)
lonval = list(dfLon.values)
idVal = list(dfFid.values)
dayVal = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
#Zip Field ID, Lat, Lng
for latrow, lonrow, idVal in zip(latval, lonval, idVal):
#send lat/lngs to DTN
url = 'https://insight.api.wdtinc.com/daily-high-temperature/' + str(latrow )+'/' + str(lonrow) +'?start='+ dayVal +'T00:00:00Z&end=' +dayVal+'T01:00:00Z'+ '&unit=fahrenheit'
r = se_.get(url, auth=('USERNAME', 'PASSWORD'), timeout= 10)
# print(r.url)
hiTemp = (json.loads(r.content)['series'][0]['value'])
strTemp = str(hiTemp)
tempDF = {'field id': [idVal],
'high temperature': [strTemp]
}
df = DataFrame(tempDF,columns= ['field id', 'high temperature'])
dfList = [df]
dfs = [df.set_index('field id') for df in dfList]
i = pd.concat(dfs, axis=1)
print i
i.to_csv(output_high_csv, encoding='utf-8', index=True)`
When I print i
my output is:
EDIT: When I use mode= 'a'
Here is my output:
使用添加模式尝试一下:
i.to_csv(output_high_csv, encoding='utf-8', index=True, mode='a')
Your code is very convoluted. I think this is a simplified version of what you're attempting. It may have mistakes but if this doesn't answer your question, try and use it to simplify the code in your question.
import pandas as pd
import requests
import csv
import json
from datetime import datetime, timedelta
from pandas import DataFrame
from pathlib import Path
DATA_PATH = Path('C:/wab')
input_csv = 'my_data.csv'
output_csv = 'outputempbeets1.csv'
se_ = requests.Session()
df = pd.read_csv(DATA_PATH / input_csv,
usecols=['lat', 'lon', 'field id'],
dtype=str)
dayVal = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
dfs = []
def get_high_temp(lat, lon, dayVal):
url = f'https://insight.api.wdtinc.com/daily-high-temperature/{lat}/{lon}'
params = {
'start': f'{dayVal}T00:00:00Z',
'end': f'{dayVal}T01:00:00Z',
'unit': 'fahrenheit',
}
response = se_.get(url,
params=params,
auth=('USERNAME', 'PASSWORD'),
timeout=10)
hi_temp = str(json.loads(response.content)['series'][0]['value'])
return hi_temp
for _, lat, lon, id_ in df.itertuples():
hi_temp = get_high_temp(lat, lon, dayVal)
temp_df = pd.DataFrame({'high temperature': [hi_temp],},
index=[id_])
dfs = dfs.append(temp_df)
pd.concat(dfs).to_csv(DATA_PATH / output_csv, encoding='utf-8')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.