External balance on goods and services (current US$)

Source: worldbank.org, 03.09.2025

Year: 2024

Flag Country Value Value change, % Rank
Angola Angola 10,829,119,878 -13% 31
Albania Albania -1,862,119,342 +53.2% 89
Argentina Argentina 15,889,079,544 -315% 25
Armenia Armenia 142,859,471 -173% 59
Australia Australia 36,729,293,880 -51.4% 13
Austria Austria 17,273,722,889 +59.6% 22
Azerbaijan Azerbaijan 6,774,058,824 -35.3% 35
Belgium Belgium -87,239,866 -97.7% 61
Benin Benin -635,776,350 -30.6% 75
Burkina Faso Burkina Faso -1,500,497,207 +4.01% 84
Bangladesh Bangladesh -26,357,644,330 +29.1% 129
Bulgaria Bulgaria 2,609,474,169 -37.9% 45
Bahamas Bahamas -581,900,000 +311% 74
Bosnia & Herzegovina Bosnia & Herzegovina -4,062,299,869 +25.3% 104
Belarus Belarus -1,359,747,051 -500% 81
Bermuda Bermuda 2,882,900,000 +25.1% 44
Brazil Brazil 10,856,517,290 -78.2% 30
Brunei Brunei 2,377,201,822 -4.8% 46
Botswana Botswana -2,887,134,465 +246% 95
Central African Republic Central African Republic -465,266,245 +24.7% 73
Canada Canada -5,745,750,821 -492% 111
Switzerland Switzerland 95,742,532,387 -5.66% 6
Chile Chile 11,845,972,478 +293% 28
China China 533,713,513,222 +39.1% 1
Côte d’Ivoire Côte d’Ivoire 458,038,706 -116% 56
Cameroon Cameroon -3,290,967,271 +67.4% 98
Congo - Kinshasa Congo - Kinshasa -3,007,630,309 +63.6% 96
Congo - Brazzaville Congo - Brazzaville 1,944,745,263 -27.8% 49
Colombia Colombia -20,688,762,303 +19.5% 124
Comoros Comoros -379,586,819 +6.25% 70
Cape Verde Cape Verde -314,959,435 -30.8% 67
Costa Rica Costa Rica 5,466,033,886 +17.9% 37
Cyprus Cyprus 1,302,134,453 +267% 51
Czechia Czechia 22,645,853,224 +32% 18
Germany Germany 179,680,569,715 -0.883% 4
Djibouti Djibouti 512,102,945 -15.7% 55
Denmark Denmark 46,428,394,342 +39.9% 11
Dominican Republic Dominican Republic -7,764,731,649 -10.4% 115
Ecuador Ecuador 4,181,712,600 +2,474% 41
Egypt Egypt -26,643,864,091 +201% 130
Spain Spain 73,742,581,336 +15.9% 8
Estonia Estonia 241,249,618 -31.8% 58
Finland Finland 1,933,131,517 +108% 50
France France -23,235,028,570 -61.9% 126
Micronesia (Federated States of) Micronesia (Federated States of) -196,400,000 +6.23% 64
Gabon Gabon 7,527,803,015 +0.523% 33
United Kingdom United Kingdom -41,250,509,365 +14% 132
Georgia Georgia -2,568,994,366 -3.14% 92
Ghana Ghana 953,399,973 -240% 53
Guinea Guinea -3,044,583,519 +79.6% 97
Gambia Gambia -768,209,323 +38% 76
Guinea-Bissau Guinea-Bissau -333,026,708 +12.5% 68
Equatorial Guinea Equatorial Guinea 1,249,393,561 -13.9% 52
Greece Greece -13,777,256,225 +19.4% 119
Guatemala Guatemala -17,612,103,505 +11.9% 123
Hong Kong SAR China Hong Kong SAR China 16,511,629,606 +639% 23
Honduras Honduras -8,906,890,933 +7.78% 116
Croatia Croatia -2,884,598,424 +86.5% 94
Haiti Haiti -3,896,207,609 -3.04% 103
Hungary Hungary 12,371,537,888 +26.7% 27
Indonesia Indonesia 25,049,322,656 -15.9% 17
India India -90,572,737,249 +18.8% 134
Ireland Ireland 243,604,877,819 +34.3% 2
Iran Iran -17,145,345,954 +12.2% 121
Iraq Iraq 778,980,385 -97% 54
Iceland Iceland -382,939,692 -892% 71
Israel Israel 13,063,808,228 -9.89% 26
Italy Italy 54,327,706,392 +65% 10
Jordan Jordan -7,712,253,521 +13.4% 114
Kenya Kenya -9,984,700,944 +5.99% 117
Cambodia Cambodia -333,665,530 +87% 69
Kiribati Kiribati -272,793,830 -1.28% 66
Libya Libya 7,334,059,295 +11.6% 34
Sri Lanka Sri Lanka -2,597,644,514 +43.8% 93
Lithuania Lithuania 4,395,860,974 +41.1% 39
Luxembourg Luxembourg 30,547,176,466 +12.3% 15
Latvia Latvia -1,114,293,374 -29.5% 78
Macao SAR China Macao SAR China 22,272,833,658 +19.5% 20
Morocco Morocco -14,185,782,126 +18.5% 120
Moldova Moldova -4,702,789,931 +18.4% 107
Madagascar Madagascar -1,345,125,479 +18.9% 80
Mexico Mexico -21,123,031,930 -18% 125
North Macedonia North Macedonia -2,189,554,295 +6.55% 90
Mali Mali -1,559,617,681 -19.4% 85
Malta Malta 4,228,108,052 +13.8% 40
Montenegro Montenegro -1,829,872,551 +30.4% 88
Mongolia Mongolia -145,038,254 -107% 63
Mozambique Mozambique -2,287,316,734 -27.6% 91
Mauritius Mauritius -1,732,340,041 +22.4% 87
Malaysia Malaysia 22,545,523,833 +10.6% 19
Namibia Namibia -3,522,381,730 +20.7% 101
Niger Niger 2,020,107,751 +1,190% 48
Nicaragua Nicaragua -3,466,084,428 +46.3% 100
Netherlands Netherlands 148,477,705,562 +15.2% 5
Norway Norway 67,203,815,432 -9.99% 9
Nepal Nepal -10,851,312,837 -4.07% 118
Nauru Nauru -127,060,360 +47.1% 62
Pakistan Pakistan -24,893,517,780 -3.58% 128
Peru Peru 16,194,388,066 +74% 24
Philippines Philippines -66,189,443,106 +7.06% 133
Poland Poland 36,658,868,368 -21.4% 14
Puerto Rico Puerto Rico 11,469,800,000 +71.9% 29
Portugal Portugal 5,612,694,745 +68.6% 36
Paraguay Paraguay -1,079,351,355 -245% 77
Palestinian Territories Palestinian Territories -5,379,400,000 -31.6% 109
Romania Romania -23,346,751,044 +39.1% 127
Russia Russia 94,019,176,890 +9.34% 7
Rwanda Rwanda -1,184,347,481 -44.5% 79
Saudi Arabia Saudi Arabia 43,606,933,333 -43.3% 12
Sudan Sudan -37,591,104 -63.4% 60
Senegal Senegal -4,867,762,305 -37% 108
Singapore Singapore 192,576,893,401 +1.79% 3
Sierra Leone Sierra Leone -1,705,671,180 +108% 86
El Salvador El Salvador -6,768,230,000 +5.67% 113
Somalia Somalia -6,577,585,550 +12.7% 112
Serbia Serbia -5,412,585,013 +53.2% 110
Slovakia Slovakia 285,784,162 -86% 57
Slovenia Slovenia 4,774,501,474 +7.82% 38
Sweden Sweden 26,794,070,912 +13.7% 16
Seychelles Seychelles -390,807,755 +27.5% 72
Chad Chad 2,242,479,007 -7.66% 47
Togo Togo -1,360,609,215 +8.95% 82
Thailand Thailand 17,650,738,217 +65.7% 21
Tunisia Tunisia -4,336,687,891 +61.6% 106
Turkey Turkey 3,551,005,971 -113% 43
Tanzania Tanzania -1,459,125,191 -51.7% 83
Uganda Uganda -4,108,584,745 -16.9% 105
Ukraine Ukraine -36,112,248,772 -4.69% 131
Uruguay Uruguay 4,093,921,667 +55.8% 42
United States United States -903,051,000,000 +13.3% 135
Uzbekistan Uzbekistan -17,469,962,042 -1.36% 122
Samoa Samoa -261,673,077 -15.9% 65
Kosovo Kosovo -3,391,281,277 +5.62% 99
South Africa South Africa 7,993,576,161 +538% 32
Zimbabwe Zimbabwe -3,739,012,677 +39% 102

                    
# 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 = 'NE.RSB.GNFS.CD'

# 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 <- 'NE.RSB.GNFS.CD'

# 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))