从OpenWeather API中提取数据
我正在尝试从OpenWeather API中提取乡村天气数据,并将其添加到数据框架中以创建CSV文件,但我遇到了一些问题。我导入数据,当我打印数据时,我可以以格式化的形式看到请求的数据(湿度,温度,风速等)。创建数据框时,我开始遇到问题。
import requests
import pandas as pd
import json
from datetime import datetime
API_key = ''
countries = ['Jamaica', 'Haiti', 'Montserrat', 'Barbados', 'Cuba', 'Dominican Republic', 'Saint Lucia', 'Antigua and Barbuda', 'Belize', 'Aruba']
for country_names in countries:
url = f'http://api.openweathermap.org/data/2.5/weather?q={country_names}&APPID={API_key}&units=imperial'
r = requests.get(url)
#if (r.status_code == 200):
data = r.json()
formatted_json = json.dumps(data, sort_keys = True, indent = 4)
caribbean_countries = []
maxtemp = []
mintemp = []
humidity = []
weather = []
windspeed = []
caribbean_countries.append(data['name'])
#name = data['name']
maxtemp.append(data['main']['temp_max'])
mintemp.append(data['main']['temp_min'])
humidity.append(data['main']['humidity'])
weather.append(data['weather'][0]['description'])
windspeed.append(data['wind']['speed'])
countries_weather_df = pd.DataFrame()
countries_weather_df['Names'] = caribbean_countries
countries_weather_df['Max_Temp'] = maxtemp
countries_weather_df['Min_Temp'] = mintemp
countries_weather_df['Humidity'] = humidity
countries_weather_df['Weather'] = weather
countries_weather_df['WindSpeed'] = windspeed
countries_weather_df
结果只显示一个国家。我如何向所有要求的国家展示并将其放入数据框架中?
我如何在数据框架中显示所有国家?
I am trying to extract country weather data from the openweather api and add it to a data frame to be able to create a csv file, but I am running into some problems. I imported the data and when I print it, I am able to see the requested data(humidity, temperature, wind speed etc.) in formatted form. I started to run into problems when I create my dataframe.
import requests
import pandas as pd
import json
from datetime import datetime
API_key = ''
countries = ['Jamaica', 'Haiti', 'Montserrat', 'Barbados', 'Cuba', 'Dominican Republic', 'Saint Lucia', 'Antigua and Barbuda', 'Belize', 'Aruba']
for country_names in countries:
url = f'http://api.openweathermap.org/data/2.5/weather?q={country_names}&APPID={API_key}&units=imperial'
r = requests.get(url)
#if (r.status_code == 200):
data = r.json()
formatted_json = json.dumps(data, sort_keys = True, indent = 4)
caribbean_countries = []
maxtemp = []
mintemp = []
humidity = []
weather = []
windspeed = []
caribbean_countries.append(data['name'])
#name = data['name']
maxtemp.append(data['main']['temp_max'])
mintemp.append(data['main']['temp_min'])
humidity.append(data['main']['humidity'])
weather.append(data['weather'][0]['description'])
windspeed.append(data['wind']['speed'])
countries_weather_df = pd.DataFrame()
countries_weather_df['Names'] = caribbean_countries
countries_weather_df['Max_Temp'] = maxtemp
countries_weather_df['Min_Temp'] = mintemp
countries_weather_df['Humidity'] = humidity
countries_weather_df['Weather'] = weather
countries_weather_df['WindSpeed'] = windspeed
countries_weather_df
The result only shows one country. How do I show all the requested countries and put it into a dataframe?
How can I show all the countries in the dataframe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在for循环之前移动
caribbean_countries
,否则它将覆盖每次迭代。您还需要为您的其他值执行此操作,否则它们也会被覆盖。You need to move
caribbean_countries
before the for loop or else it'll overwrite each iteration. You also need to do this for your other values or they'll be overwritten too.