[英]Save data from API to csv in Python Pandas
I need help with saving data I read with API key to csv. 我需要保存使用API密钥读取的数据到csv的帮助。 The code I have is below:
我的代码如下:
import requests
import pandas as pd
def get_precip(gooddate):
urlstart = 'http://api.wunderground.com/api/API_KEY/history_'
urlend = '/q/Switzerland/Zurich.json'
url = urlstart + str(gooddate) + urlend
data = requests.get(url).json()
for summary in data['history']['dailysummary']:
abc = ','.join((gooddate,summary['date']['year'],summary['date']['mon'],summary['date']['mday'],summary['precipm'], summary['maxtempm'], summary['meantempm'],summary['mintempm']))
df = pd.DataFrame(data=abc)
df.to_csv('/home/user/Desktop/2013_weather.csv', index=False)
if __name__ == "__main__":
from datetime import date
from dateutil.rrule import rrule, DAILY
a = date(2013, 1, 1)
b = date(2013, 12, 31)
for dt in rrule(DAILY, dtstart=a, until=b):
get_precip(dt.strftime("%Y%m%d"))
I'm sure that can't work this way, because it need to be saved into some list or dictionary before transform into dataframe, but not sure how to do that this time. 我敢肯定这种方式无法正常工作,因为在转换为数据框之前需要将其保存到某些列表或字典中,但不确定这次如何执行此操作。 If save it to the list it will give me just one row?
如果将其保存到列表中,它将只给我一行? Any help is welcomed.
欢迎任何帮助。 Thanks.
谢谢。
I think you can return tuples from get_precip
, append them to list
and use DataFrame
constructor: 我认为您可以从
get_precip
返回元组,将它们附加到list
并使用DataFrame
构造函数:
def get_precip(gooddate):
urlstart = 'http://api.wunderground.com/api/API_KEY/history_'
urlend = '/q/Switzerland/Zurich.json'
url = urlstart + str(gooddate) + urlend
data = requests.get(url).json()
for summary in data['history']['dailysummary']:
return (gooddate,summary['date']['year'],summary['date']['mon'],summary['date']['mday'],summary['precipm'], summary['maxtempm'], summary['meantempm'],summary['mintempm'])
if __name__ == "__main__":
from datetime import date
from dateutil.rrule import rrule, DAILY
a = date(2013, 1, 1)
b = date(2013, 12, 31)
L = []
for dt in rrule(DAILY, dtstart=a, until=b):
tup = get_precip(dt.strftime("%Y%m%d"))
L.append(tup)
what is same as: 与什么相同:
L = [get_precip(dt.strftime("%Y%m%d")) for dt in rrule(DAILY, dtstart=a, until=b)]
cols = ['date','date.year','date.mon','date.mday','precipm','maxtempm',
'meantempm','mintempm']
df = pd.DataFrame(L, columns=cols)
print (df.head())
date date.year date.mon date.mday precipm maxtempm meantempm mintempm
0 20130101 2013 01 01 0.0 7 2 -2
1 20130102 2013 01 02 0.0 5 2 -3
2 20130103 2013 01 03 0.0 4 0 -3
3 20130104 2013 01 04 0.0 7 5 3
df.to_csv('/home/user/Desktop/2013_weather.csv', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.