从OpenWeather API中提取数据

发布于 2025-01-22 03:20:02 字数 1580 浏览 3 评论 0原文

我正在尝试从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2025-01-29 03:20:03

您需要在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    
        

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.

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    
        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文