Extracting GitHub Repository Statistics into a Pandas DataFrame

To fetch live repository metrics (such as stars, forks, and open issues) from a public Representational State Transfer Application Programming Interface (REST API) and convert them directly into a Pandas DataFrame, use the Python requests library. Run this snippet instantly inside Google Colab:

import requests
import pandas as pd

# Fetch public repository details from GitHub REST API
url = "https://api.github.com/repos/pandas-dev/pandas"
headers = {"Accept": "application/vnd.github.v3+json"}
response = requests.get(url, headers=headers)

if response.status_code == 200:
    repo_data = response.json()
    
    # Extract key metrics into a DataFrame
    df = pd.DataFrame([{
        'Repository': repo_data['full_name'],
        'Stars': repo_data['stargazers_count'],
        'Forks': repo_data['forks_count'],
        'Open_Issues': repo_data['open_issues_count'],
        'Language': repo_data['language']
    }])
    
    print(df)
else:
    print(f"Error fetching GitHub data: {response.status_code}")
What This Script Does & Data Overview

This script connects directly to GitHub's public developer interface to retrieve real-time project metrics, such as star counts, fork statistics, and open issues for any open-source repository. It structures the JSON response into a clean Pandas DataFrame, making it immediately useful for project reporting, comparison, and long-term data archiving.

Prerequisites & Required Libraries Top ↑

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

!pip install --upgrade requests pandas

Flattening Nested Contributor or Release Data Top ↑

Endpoints returning lists of items (such as repository contributors or recent issues) require pd.json_normalize() to flatten nested JSON structures into tabular columns:

# Fetch top contributors for a repository
url = "https://api.github.com/repos/pandas-dev/pandas/contributors?per_page=5"
response = requests.get(url)

if response.status_code == 200:
    contributors = response.json()
    df_contrib = pd.json_normalize(contributors)
    print(df_contrib[['login', 'contributions']])

Handling Rate Limits & Headers Top ↑

GitHub imposes unauthenticated rate limits (typically 60 requests per hour). Always include appropriate User-Agent headers and handle response codes gracefully:

headers = {
    "User-Agent": "Plus2Net-Data-Tutorial",
    "Accept": "application/vnd.github.v3+json"
}
response = requests.get("https://api.github.com/rate_limit", headers=headers)
print(response.json())

Saving Repository Metrics (SQLite, CSV, Excel) Top ↑

Once your repository statistics are loaded into a DataFrame, you can persist them across multiple storage formats:

import sqlite3

# Save to CSV
df.to_csv("github_repo_stats.csv", index=False)

# Save to SQLite
conn = sqlite3.connect("github_data.db")
df.to_sql("repo_metrics", conn, if_exists="replace", index=False)
conn.close()
print("GitHub metrics successfully saved!")
Interactive Companion Jupyter Notebook

Explore live GitHub API extraction code cells, test rate-limit checks, and run self-validating data assertions in Google Colab.

View on GitHub Open In Colab

Frequently Asked Questions Top ↑

For public repositories and rate limits under 60 requests per hour, an access token is optional. For heavier usage, you can pass a Bearer token in your request headers.

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