Home > OS >  extracting data from openweather api
extracting data from openweather api

Time:04-16

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?

CodePudding user response:

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    
        
  • Related