从开放天气中提取数据 api
extracting data from 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
结果只显示一个国家。如何显示所有请求的国家并将其放入数据框中?
如何在数据框中显示所有国家/地区?
您需要在 for 循环之前移动 caribbean_countries
,否则它会覆盖每次迭代。您还需要为其他值执行此操作,否则它们也会被覆盖。
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']
caribbean_countries = []
maxtemp = []
mintemp = []
humidity = []
weather = []
windspeed = []
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.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
我正在尝试从 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
结果只显示一个国家。如何显示所有请求的国家并将其放入数据框中?
如何在数据框中显示所有国家/地区?
您需要在 for 循环之前移动 caribbean_countries
,否则它会覆盖每次迭代。您还需要为其他值执行此操作,否则它们也会被覆盖。
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']
caribbean_countries = []
maxtemp = []
mintemp = []
humidity = []
weather = []
windspeed = []
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.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