我认为您可以从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'])
^{pr2}$
什么是相同的: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)