Extracting Live Weather API Data into a Pandas DataFrame

To fetch live meteorological data from a public Representational State Transfer Application Programming Interface (REST API) without requiring an API key, use the Python requests library combined with Pandas. Run this snippet instantly inside Google Colab:

import requests
import pandas as pd

# Fetch live hourly weather forecast endpoint (Open-Meteo)
url = "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&hourly=temperature_2m,relative_humidity_2m"
response = requests.get(url)

if response.status_code == 200:
    data = response.json()
    hourly_data = data['hourly']
    
    # Build DataFrame from time-series lists
    df = pd.DataFrame({
        'Time': pd.to_datetime(hourly_data['time']),
        'Temperature_C': hourly_data['temperature_2m'],
        'Humidity_Percent': hourly_data['relative_humidity_2m']
    })
    
    print(df.head())
else:
    print(f"Error fetching weather data: {response.status_code}")
What This Script Does & Data Overview

This script connects directly to a public meteorological web service to pull real-time hourly weather forecasts, including air temperature and relative humidity values for specified geographic coordinates. It converts the incoming JSON time series into a clean, structured Pandas table, enabling instant data analysis, temperature trend calculations, and export to local databases or flat files.

Prerequisites & Required Libraries Top ↑

Ensure you have the required libraries installed in your Python environment or Google Colab notebook:

!pip install --upgrade requests pandas

Handling Time-Series & Datetime Indexing Top ↑

Weather APIs return timestamp arrays as string lists. Converting them using pd.to_datetime() and setting them as the DataFrame index allows powerful time-based slicing and filtering:

import requests
import pandas as pd

url = "https://api.open-meteo.com/v1/forecast?latitude=28.61&longitude=77.20&hourly=temperature_2m"
response = requests.get(url)
data = response.json()

df = pd.DataFrame({
    'Timestamp': pd.to_datetime(data['hourly']['time']),
    'Temp': data['hourly']['temperature_2m']
})

# Set timestamp as index for time-series operations
df.set_index('Timestamp', inplace=True)
print(df.head())

Resampling Meteorological Data Top ↑

You can aggregate hourly readings into daily summary statistics using Pandas resample():

# Calculate daily maximum and minimum temperatures
daily_summary = df.resample('D').agg({'Temp': ['max', 'min', 'mean']})
print(daily_summary.head())

Saving Weather Data (SQLite, CSV, Excel) Top ↑

Persist your processed weather forecasts across various formats:

import sqlite3

# Save to CSV
df.to_csv("weather_forecast.csv")

# Save to SQLite
conn = sqlite3.connect("weather.db")
df.to_sql("hourly_forecast", conn, if_exists="replace", index=True)
conn.close()
print("Weather data successfully persisted!")
Interactive Companion Jupyter Notebook

Explore live weather API extraction code cells, test datetime resampling, and run self-validating data assertions in Google Colab.

View on GitHub Open In Colab

Frequently Asked Questions Top ↑

No. Open-Meteo is a free open-source weather API that requires zero API keys or authentication for non-commercial use.

Related Python & Pandas Tutorials




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer