โ† Back to articles

Terminal Tools for GDP Data Access

Path: ไธ€ etc ไธ€/Economics/Terminal Tools for GDP Data Access.mdUpdated: 2/3/2026

Terminal Tools for GDP Data Access

Command-line tools provide programmatic access to GDP data from the World Bank, IMF, and other sources. These tools enable automated data retrieval, analysis pipelines, and integration with research workflows.

Python: wbgapi Package

Installation

bash
# Using pip
pip install wbgapi

# Using conda
conda install -c conda-forge wbgapi

Basic Usage

python
import wbgapi as wb

# Search for GDP-related indicators
indicators = wb.series.info(q='GDP')
for ind in indicators:
    print(f"{ind['id']}: {ind['value']}")

# Get GDP data for specific country
china_gdp = wb.data.fetch(
    'NY.GDP.MKTP.CD',  # GDP indicator
    'CHN',              # China
    time=range(1980, 2025)
)

# Convert to pandas DataFrame
import pandas as pd
df = wb.data.DataFrame('NY.GDP.PCAP.CD', ['US', 'CN', 'IN'], 
                        time=range(2000, 2025))

Advanced Queries

python
# Get GDP for all countries in a specific year
gdp_2024 = wb.data.DataFrame('NY.GDP.MKTP.CD', wb.economy.list(), 
                              time=2024)

# Filter by region
east_asia = wb.economy.list(region='EAS')
ea_gdp = wb.data.DataFrame('NY.GDP.PCAP.CD', east_asia, 
                            time=range(2010, 2025))

# Multiple indicators
indicators = {
    'NY.GDP.MKTP.CD': 'GDP',
    'NY.GDP.PCAP.CD': 'GDP_per_capita',
    'NY.GDP.MKTP.KD.ZG': 'GDP_growth'
}
multi_data = wb.data.DataFrame(indicators.keys(), 'CHN', 
                                time=range(2000, 2025))

Export Options

python
# Export to CSV
df.to_csv('gdp_data.csv')

# Export to Excel
df.to_excel('gdp_data.xlsx', sheet_name='GDP')

# Export to JSON
df.to_json('gdp_data.json', orient='records')

R: wbstats Package

Installation

r
# Install from CRAN
install.packages("wbstats")

# Load library
library(wbstats)

Basic Usage

r
# Search indicators
gdp_indicators <- wb_search("GDP per capita")
head(gdp_indicators)

# Fetch data for multiple countries
gdp_data <- wb_data(
  indicator = "NY.GDP.PCAP.CD",
  country = c("US", "CN", "IN", "BR"),
  start_date = 1980,
  end_date = 2024
)

# View structure
str(gdp_data)

Visualization with ggplot2

r
library(ggplot2)
library(dplyr)

# Fetch and plot GDP per capita trends
countries <- c("US", "CN", "IN", "JP", "DE")
gdp_pc <- wb_data(
  indicator = "NY.GDP.PCAP.CD",
  country = countries,
  start_date = 2000,
  end_date = 2024
)

ggplot(gdp_pc, aes(x = date, y = value, color = country)) +
  geom_line(size = 1.2) +
  labs(
    title = "GDP Per Capita Comparison (2000-2024)",
    x = "Year",
    y = "GDP Per Capita (current US$)",
    color = "Country"
  ) +
  theme_minimal()

Advanced R Workflows

r
# Get GDP growth rates for all countries
growth_data <- wb_data(
  indicator = "NY.GDP.MKTP.KD.ZG",
  country = "countries_only",
  start_date = 2010,
  end_date = 2024
)

# Calculate average growth by region
library(tidyverse)
avg_growth <- growth_data %>%
  group_by(iso3c) %>%
  summarize(avg_growth = mean(value, na.rm = TRUE)) %>%
  arrange(desc(avg_growth))

# Export results
write.csv(avg_growth, "average_growth_rates.csv", row.names = FALSE)

Direct API Access with curl

Basic HTTP Requests

bash
# Get GDP for Brazil in 2024
curl "https://api.worldbank.org/v2/country/br/indicator/NY.GDP.MKTP.CD?date=2024&format=json" \
  | jq '.[] | select(type=="array")'

# Get China GDP per capita 1980-2024
curl "https://api.worldbank.org/v2/country/cn/indicator/NY.GDP.PCAP.CD?date=1980:2024&format=json" \
  | jq '.[] | select(type=="array") | .[] | {year: .date, value: .value}'

# Get GDP for multiple countries
curl "https://api.worldbank.org/v2/country/us;cn;in/indicator/NY.GDP.MKTP.CD?date=2024&format=json" \
  | jq '.[] | select(type=="array")'

Pagination Handling

bash
# API returns max 50 results per page
# Use per_page and page parameters
curl "https://api.worldbank.org/v2/country/all/indicator/NY.GDP.MKTP.CD?date=2024&format=json&per_page=100&page=1" \
  | jq '.'

Shell Script for Bulk Download

bash
#!/bin/bash
# download_gdp.sh - Download GDP data for multiple countries

COUNTRIES=("us" "cn" "in" "jp" "de" "gb" "fr")
INDICATOR="NY.GDP.PCAP.CD"
START_YEAR=2000
END_YEAR=2024

for country in "${COUNTRIES[@]}"; do
  echo "Downloading data for $country..."
  curl -s "https://api.worldbank.org/v2/country/$country/indicator/$INDICATOR?date=$START_YEAR:$END_YEAR&format=json" \
    | jq '.[] | select(type=="array")' > "${country}_gdp.json"
  sleep 1  # Rate limiting
done

echo "Download complete!"

Common GDP Indicator Codes

CodeDescriptionUnits
NY.GDP.MKTP.CDGDP (current US$)USD
NY.GDP.PCAP.CDGDP per capita (current US$)USD
NY.GDP.MKTP.PP.CDGDP, PPP (current international $)International $
NY.GDP.PCAP.PP.CDGDP per capita, PPPInternational $
NY.GDP.MKTP.KDGDP (constant 2015 US$)USD
NY.GDP.MKTP.KD.ZGGDP growth (annual %)Percentage
NY.GDP.DEFL.ZSGDP deflator (annual %)Percentage

Data Processing Workflows

Python Pipeline Example

python
import wbgapi as wb
import pandas as pd
import matplotlib.pyplot as plt

# 1. Fetch data
countries = ['US', 'CN', 'IN', 'JP', 'DE']
indicator = 'NY.GDP.PCAP.CD'
years = range(1980, 2025)

df = wb.data.DataFrame(indicator, countries, time=years)

# 2. Clean data
df = df.dropna()  # Remove missing values
df = df.transpose()  # Countries as columns

# 3. Calculate growth rates
growth_df = df.pct_change() * 100

# 4. Visualize
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))

# Plot GDP levels
df.plot(ax=ax1, title='GDP Per Capita (1980-2024)')
ax1.set_ylabel('Current US$')
ax1.legend(loc='best')

# Plot growth rates
growth_df.plot(ax=ax2, title='GDP Per Capita Growth Rates')
ax2.set_ylabel('Annual % Change')
ax2.legend(loc='best')

plt.tight_layout()
plt.savefig('gdp_analysis.png', dpi=300)

R Pipeline Example

r
library(wbstats)
library(tidyverse)

# 1. Fetch data
countries <- c("US", "CN", "IN", "JP", "DE")
gdp_data <- wb_data(
  indicator = "NY.GDP.PCAP.CD",
  country = countries,
  start_date = 1980,
  end_date = 2024
)

# 2. Calculate summary statistics
summary_stats <- gdp_data %>%
  group_by(country) %>%
  summarize(
    mean_gdp = mean(value, na.rm = TRUE),
    median_gdp = median(value, na.rm = TRUE),
    min_gdp = min(value, na.rm = TRUE),
    max_gdp = max(value, na.rm = TRUE),
    growth = (last(value) - first(value)) / first(value) * 100
  )

# 3. Export results
write_csv(summary_stats, "gdp_summary.csv")

# 4. Visualize
ggplot(gdp_data, aes(x = date, y = value, color = country)) +
  geom_line(size = 1) +
  scale_y_log10() +
  labs(
    title = "GDP Per Capita Growth (1980-2024)",
    subtitle = "Log scale",
    x = "Year",
    y = "GDP Per Capita (current US$, log scale)"
  ) +
  theme_minimal()

ggsave("gdp_growth_log.png", width = 12, height = 6, dpi = 300)

Performance Optimization

Caching Data

python
# Python: Cache downloaded data
import pickle

def fetch_with_cache(indicator, countries, years, cache_file='gdp_cache.pkl'):
    try:
        with open(cache_file, 'rb') as f:
            data = pickle.load(f)
        print("Loaded from cache")
        return data
    except FileNotFoundError:
        data = wb.data.DataFrame(indicator, countries, time=years)
        with open(cache_file, 'wb') as f:
            pickle.dump(data, f)
        print("Fetched fresh data")
        return data

# Usage
df = fetch_with_cache('NY.GDP.PCAP.CD', ['US', 'CN'], range(2000, 2025))

Batch Processing

python
# Download data for multiple indicators efficiently
indicators = [
    'NY.GDP.MKTP.CD',
    'NY.GDP.PCAP.CD',
    'NY.GDP.MKTP.KD.ZG',
    'SP.POP.TOTL'
]

all_data = {}
for ind in indicators:
    print(f"Fetching {ind}...")
    all_data[ind] = wb.data.DataFrame(ind, 'CHN', time=range(2000, 2025))

# Combine into single DataFrame
combined = pd.concat(all_data, axis=1)
combined.columns = indicators

Troubleshooting

Connection Issues

python
# Set timeout for slow connections
import wbgapi as wb
wb.timeout = 30  # seconds

# Retry logic
import time

def fetch_with_retry(indicator, country, max_retries=3):
    for attempt in range(max_retries):
        try:
            return wb.data.fetch(indicator, country)
        except Exception as e:
            print(f"Attempt {attempt + 1} failed: {e}")
            time.sleep(2)
    raise Exception("Max retries exceeded")

Missing Data Handling

python
# Check data availability
availability = wb.series.metadata.get('NY.GDP.PCAP.CD')
print(f"Coverage: {availability}")

# Fill missing values
df_filled = df.interpolate(method='linear')  # Linear interpolation
df_filled = df.fillna(method='ffill')  # Forward fill

Related Topics

  • Accessing and Analyzing Global GDP Data - Overview of GDP data sources
  • World GDP Rankings 2024-2025 - Current country rankings
  • Historical GDP Data (Maddison Project) - Long-run historical data

Links

wbgapi Documentation

wbstats R Package

World Bank API Documentation