Expense (% of GDP)

Source: worldbank.org, 01.09.2025

Year: 2023

Flag Country Value Value change, % Rank
Albania Albania 25.4 -6.03% 47
United Arab Emirates United Arab Emirates 3.83 -1.15% 85
Argentina Argentina 21.1 -3.6% 57
Armenia Armenia 21 -0.607% 58
Austria Austria 46.5 -2.17% 6
Burkina Faso Burkina Faso 19.4 -14.1% 63
Bulgaria Bulgaria 35.7 -9.24% 22
Bahamas Bahamas 20.3 -8.86% 60
Bosnia & Herzegovina Bosnia & Herzegovina 35.3 +5.6% 23
Belarus Belarus 28.5 +0.702% 40
Brazil Brazil 32.4 +2.5% 33
Botswana Botswana 23.9 -4.76% 52
Canada Canada 19.3 +5.52% 64
Switzerland Switzerland 16.7 +1.56% 76
Chile Chile 24.6 +1.88% 49
Côte d’Ivoire Côte d’Ivoire 18.1 -1.47% 67
Colombia Colombia 33.3 +2.92% 31
Costa Rica Costa Rica 28.9 +0.512% 39
Cyprus Cyprus 39.8 +8.97% 12
Denmark Denmark 33.1 +3.5% 32
Dominican Republic Dominican Republic 18.1 +0.0516% 69
Spain Spain 34 -3.85% 28
Estonia Estonia 37.9 +7.29% 16
Ethiopia Ethiopia 6.12 -16.2% 84
Finland Finland 44.9 +18.5% 7
Fiji Fiji 27.9 -6.85% 43
France France 47.3 -3.09% 4
United Kingdom United Kingdom 41.9 +0.78% 10
Georgia Georgia 25.2 +1.22% 48
Guinea-Bissau Guinea-Bissau 15.5 +6.77% 79
Greece Greece 47.2 -6.72% 5
Guatemala Guatemala 13.5 -3.52% 80
Croatia Croatia 38.7 +2.23% 15
Iceland Iceland 32.3 -2.4% 34
Israel Israel 37.4 +6.22% 18
Italy Italy 47.5 -1.74% 3
Jordan Jordan 28.4 -1.25% 41
Kazakhstan Kazakhstan 17.4 +7.92% 70
Kenya Kenya 27.7 +10.5% 44
Kyrgyzstan Kyrgyzstan 23.7 -0.533% 53
Cambodia Cambodia 12.9 +9.96% 81
Kiribati Kiribati 84.6 +3.27% 1
South Korea South Korea 30.5 -9.33% 35
Sri Lanka Sri Lanka 18.1 +15.1% 68
Lithuania Lithuania 34.1 +0.158% 27
Luxembourg Luxembourg 42.8 +4.69% 9
Latvia Latvia 34.4 -5.45% 25
Macao SAR China Macao SAR China 19.5 -60.2% 62
Morocco Morocco 26.9 -4.6% 46
Moldova Moldova 34.5 +6.91% 24
Madagascar Madagascar 11.2 +12.2% 82
Mexico Mexico 22.5 +0.691% 56
North Macedonia North Macedonia 33.6 +4.25% 30
Malta Malta 34.3 -4.46% 26
Mauritius Mauritius 29.1 +2.93% 38
Malaysia Malaysia 17 -4.95% 73
Namibia Namibia 36.5 +2.18% 20
Nicaragua Nicaragua 16.3 -2.44% 78
Netherlands Netherlands 39.3 -0.126% 13
Norway Norway 35.9 +22.5% 21
Philippines Philippines 16.9 -9.28% 75
Papua New Guinea Papua New Guinea 20.3 +4.5% 61
Poland Poland 39.1 +8.42% 14
Portugal Portugal 37.5 -4.62% 17
Paraguay Paraguay 19 +14.6% 65
Russia Russia 29.6 -5.71% 37
Rwanda Rwanda 18.6 -5.08% 66
Saudi Arabia Saudi Arabia 24.2 +10.2% 51
Senegal Senegal 23.6 +1.9% 54
Singapore Singapore 17.3 +16.7% 71
El Salvador El Salvador 27.7 +1.04% 45
Somalia Somalia 0.000233 +9.42% 86
Slovenia Slovenia 42.9 -1.36% 8
Togo Togo 17 +5.19% 74
Thailand Thailand 20.8 -6.59% 59
Tajikistan Tajikistan 11 +3.07% 83
Tonga Tonga 40.3 -6.16% 11
Turkey Turkey 33.7 +13.6% 29
Tanzania Tanzania 17 +1.09% 72
Uganda Uganda 16.6 -2.12% 77
Ukraine Ukraine 65.6 +13.2% 2
Uruguay Uruguay 37 +2.02% 19
United States United States 24.5 -0.588% 50
Uzbekistan Uzbekistan 23.4 +1.44% 55
Vanuatu Vanuatu 28.1 -10.8% 42
Samoa Samoa 29.7 -12.6% 36

                    
# Install missing packages
import sys
import subprocess

def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])

# Required packages
for package in ['wbdata', 'country_converter']:
try:
__import__(package)
except ImportError:
install(package)

# Import libraries
import wbdata
import country_converter as coco
from datetime import datetime

# Define World Bank indicator code
dataset_code = 'GC.XPN.TOTL.GD.ZS'

# Download data from World Bank API
data = wbdata.get_dataframe({dataset_code: 'value'},
date=(datetime(1960, 1, 1), datetime.today()),
parse_dates=True,
keep_levels=True).reset_index()

# Extract year
data['year'] = data['date'].dt.year

# Convert country names to ISO codes using country_converter
cc = coco.CountryConverter()
data['iso2c'] = cc.convert(names=data['country'], to='ISO2', not_found=None)
data['iso3c'] = cc.convert(names=data['country'], to='ISO3', not_found=None)

# Filter out rows where ISO codes could not be matched — likely not real countries
data = data[data['iso2c'].notna() & data['iso3c'].notna()]

# Sort for calculation
data = data.sort_values(['iso3c', 'year'])

# Calculate YoY absolute and percent change
data['value_change'] = data.groupby('iso3c')['value'].diff()
data['value_change_percent'] = data.groupby('iso3c')['value'].pct_change() * 100

# Calculate ranks (higher GDP per capita = better rank)
data['rank'] = data.groupby('year')['value'].rank(ascending=False, method='dense')

# Calculate rank change from previous year
data['rank_change'] = data.groupby('iso3c')['rank'].diff()

# Select desired columns
final_df = data[['country', 'iso2c', 'iso3c', 'year', 'value',
'value_change', 'value_change_percent', 'rank', 'rank_change']].copy()

# Optional: Add labels as metadata (could be useful for export or UI)
column_labels = {
'country': 'Country name',
'iso2c': 'ISO 2-letter country code',
'iso3c': 'ISO 3-letter country code',
'year': 'Year',
'value': 'GDP per capita (current US$)',
'value_change': 'Year-over-Year change in value',
'value_change_percent': 'Year-over-Year percent change in value',
'rank': 'Country rank by GDP per capita (higher = richer)',
'rank_change': 'Change in rank from previous year'
}

# Display first few rows
print(final_df.head(10))

# Optional: Save to CSV
#final_df.to_csv("gdp_per_capita_cleaned.csv", index=False)
                    
                
                    
# Check and install required packages
required_packages <- c("WDI", "countrycode", "dplyr")

for (pkg in required_packages) {
  if (!requireNamespace(pkg, quietly = TRUE)) {
    install.packages(pkg)
  }
}

# Load the necessary libraries
library(WDI)
library(dplyr)
library(countrycode)

# Define the dataset code (World Bank indicator code)
dataset_code <- 'GC.XPN.TOTL.GD.ZS'

# Download data using WDI package
dat <- WDI(indicator = dataset_code)

# Filter only countries using 'is_country' from countrycode
# This uses iso2c to identify whether the entry is a recognized country
dat <- dat %>%
  filter(countrycode(iso2c, origin = 'iso2c', destination = 'country.name', warn = FALSE) %in%
           countrycode::codelist$country.name.en)

# Ensure dataset is ordered by country and year
dat <- dat %>%
  arrange(iso3c, year)

# Rename the dataset_code column to "value" for easier manipulation
dat <- dat %>%
  rename(value = !!dataset_code)

# Calculate year-over-year (YoY) change and percentage change
dat <- dat %>%
  group_by(iso3c) %>%
  mutate(
    value_change = value - lag(value),                              # Absolute change from previous year
    value_change_percent = 100 * (value - lag(value)) / lag(value) # Percent change from previous year
  ) %>%
  ungroup()

# Calculate rank by year (higher value => higher rank)
dat <- dat %>%
  group_by(year) %>%
  mutate(rank = dense_rank(desc(value))) %>% # Rank countries by descending value
  ungroup()

# Calculate rank change (positive = moved up, negative = moved down)
dat <- dat %>%
  group_by(iso3c) %>%
  mutate(rank_change = rank - lag(rank)) %>% # Change in rank compared to previous year
  ungroup()

# Select and reorder final columns
final_data <- dat %>%
  select(
    country,
    iso2c,
    iso3c,
    year,
    value,
    value_change,
    value_change_percent,
    rank,
    rank_change
  )

# Add labels (variable descriptions)
attr(final_data$country, "label") <- "Country name"
attr(final_data$iso2c, "label") <- "ISO 2-letter country code"
attr(final_data$iso3c, "label") <- "ISO 3-letter country code"
attr(final_data$year, "label") <- "Year"
attr(final_data$value, "label") <- "GDP per capita (current US$)"
attr(final_data$value_change, "label") <- "Year-over-Year change in value"
attr(final_data$value_change_percent, "label") <- "Year-over-Year percent change in value"
attr(final_data$rank, "label") <- "Country rank by GDP per capita (higher = richer)"
attr(final_data$rank_change, "label") <- "Change in rank from previous year"

# Print the first few rows of the final dataset
print(head(final_data, 10))