Population in urban agglomerations of more than 1 million

Source: worldbank.org, 19.12.2024

Year: 2023

Flag Country Value Value change, % Rank
Afghanistan Afghanistan 4,588,666 +2.93% 54
Angola Angola 9,292,336 +3.8% 33
United Arab Emirates United Arab Emirates 6,405,440 +1.84% 44
Argentina Argentina 19,922,589 +0.866% 19
Armenia Armenia 1,094,813 +0.255% 118
Australia Australia 16,345,586 +1.35% 25
Austria Austria 1,975,271 +0.778% 89
Azerbaijan Azerbaijan 2,432,304 +1.3% 79
Belgium Belgium 3,179,207 +0.536% 67
Burkina Faso Burkina Faso 3,203,923 +4.85% 65
Bangladesh Bangladesh 28,589,276 +3.1% 12
Bulgaria Bulgaria 1,288,114 +0.103% 112
Belarus Belarus 2,057,257 +0.424% 86
Bolivia Bolivia 5,155,983 +1.88% 52
Brazil Brazil 92,634,332 +0.975% 4
Canada Canada 17,958,253 +1.04% 22
Switzerland Switzerland 1,431,538 +0.839% 106
Chile Chile 6,903,392 +0.677% 41
China China 441,275,113 +2.37% 1
Côte d’Ivoire Côte d’Ivoire 5,686,350 +3.09% 48
Cameroon Cameroon 8,572,487 +3.74% 36
Congo - Kinshasa Congo - Kinshasa 25,107,076 +4.41% 15
Congo - Brazzaville Congo - Brazzaville 3,974,120 +3.29% 56
Colombia Colombia 23,292,495 +1.19% 16
Costa Rica Costa Rica 1,461,989 +1.43% 104
Cuba Cuba 2,148,930 +0.151% 84
Czechia Czechia 1,323,339 +0.399% 110
Germany Germany 8,081,589 +0.242% 38
Denmark Denmark 1,381,005 +0.794% 108
Dominican Republic Dominican Republic 3,523,890 +1.92% 59
Algeria Algeria 2,901,810 +1.68% 74
Ecuador Ecuador 5,099,461 +1.57% 53
Egypt Egypt 27,771,678 +1.98% 13
Spain Spain 12,438,730 +0.539% 28
Ethiopia Ethiopia 5,460,591 +4.45% 50
Finland Finland 1,337,786 +0.755% 109
France France 15,676,297 +0.595% 26
United Kingdom United Kingdom 18,730,964 +0.918% 21
Georgia Georgia 1,082,245 +0.175% 121
Ghana Ghana 6,428,311 +3.09% 43
Guinea Guinea 2,110,937 +3.05% 85
Greece Greece 3,154,463 +0.0216% 69
Guatemala Guatemala 3,095,099 +1.93% 70
Hong Kong SAR China Hong Kong SAR China 7,684,801 +0.544% 39
Honduras Honduras 1,568,025 +2.71% 102
Haiti Haiti 2,987,455 +2.48% 72
Hungary Hungary 1,778,052 +0.16% 91
Indonesia Indonesia 39,602,968 +2.05% 8
India India 236,685,013 +2.48% 2
Ireland Ireland 1,270,172 +1.13% 114
Iran Iran 22,749,121 +1.29% 17
Iraq Iraq 10,951,449 +2.71% 31
Israel Israel 5,595,284 +1.59% 49
Italy Italy 11,451,569 +0.224% 29
Jordan Jordan 2,232,240 +1.03% 82
Japan Japan 81,280,502 -0.177% 5
Kazakhstan Kazakhstan 3,278,581 +2.08% 64
Kenya Kenya 6,765,556 +3.96% 42
Cambodia Cambodia 2,281,198 +3.15% 81
South Korea South Korea 26,257,208 +0.287% 14
Kuwait Kuwait 3,297,759 +1.83% 62
Lebanon Lebanon 2,421,354 -0.485% 80
Liberia Liberia 1,678,020 +3.42% 95
Libya Libya 1,183,292 +0.635% 116
Morocco Morocco 8,456,442 +1.68% 37
Madagascar Madagascar 3,872,264 +4.66% 57
Mexico Mexico 54,798,004 +1.35% 6
Mali Mali 2,929,373 +3.99% 73
Myanmar (Burma) Myanmar (Burma) 7,142,101 +1.81% 40
Mongolia Mongolia 1,672,627 +1.71% 97
Mozambique Mozambique 3,015,198 +2.7% 71
Mauritania Mauritania 1,491,958 +4.22% 103
Malawi Malawi 1,276,316 +4.42% 113
Malaysia Malaysia 8,621,724 +2.4% 35
Niger Niger 1,437,233 +3.85% 105
Nigeria Nigeria 38,722,546 +3.8% 9
Nicaragua Nicaragua 1,094,510 +1.05% 119
Netherlands Netherlands 2,192,037 +0.526% 83
Norway Norway 1,085,992 +1.39% 120
Nepal Nepal 1,571,010 +3.28% 101
New Zealand New Zealand 1,673,220 +1.26% 96
Oman Oman 1,650,319 +1.71% 98
Pakistan Pakistan 48,676,070 +2.65% 7
Panama Panama 1,976,866 +2.01% 88
Peru Peru 11,204,382 +1.45% 30
Philippines Philippines 16,616,489 +1.86% 24
Poland Poland 1,797,516 +0.166% 90
Puerto Rico Puerto Rico 2,439,564 -0.121% 78
North Korea North Korea 3,157,538 +0.798% 68
Portugal Portugal 4,325,188 +0.434% 55
Paraguay Paraguay 3,510,511 +1.69% 60
Romania Romania 1,776,385 -0.499% 92
Russia Russia 34,192,894 +0.296% 10
Rwanda Rwanda 1,247,551 +3.25% 115
Saudi Arabia Saudi Arabia 17,597,161 +1.81% 23
Sudan Sudan 6,344,348 +2.99% 45
Senegal Senegal 3,429,536 +3.11% 61
Singapore Singapore 6,080,859 +0.684% 47
Sierra Leone Sierra Leone 1,309,168 +2.91% 111
El Salvador El Salvador 1,116,052 +0.484% 117
Somalia Somalia 2,610,483 +4.53% 75
Serbia Serbia 1,408,144 +0.21% 107
Sweden Sweden 1,700,066 +1.25% 94
Syria Syria 6,231,225 +3.88% 46
Chad Chad 1,592,324 +3.9% 99
Togo Togo 1,981,615 +2.91% 87
Thailand Thailand 15,096,423 +1.49% 27
Tunisia Tunisia 2,475,446 +1.48% 77
Turkey Turkey 32,815,076 +1.45% 11
Tanzania Tanzania 9,086,619 +5.05% 34
Uganda Uganda 3,846,102 +5.32% 58
Ukraine Ukraine 5,445,557 +0.0738% 51
Uruguay Uruguay 1,774,396 +0.405% 93
United States United States 157,903,565 +0.888% 3
Uzbekistan Uzbekistan 2,603,243 +1.15% 76
Venezuela Venezuela 9,820,353 +1.04% 32
Vietnam Vietnam 19,083,031 +3.14% 20
Yemen Yemen 3,292,497 +3.48% 63
South Africa South Africa 22,548,654 +1.89% 18
Zambia Zambia 3,181,250 +4.58% 66
Zimbabwe Zimbabwe 1,578,128 +1.31% 100

                    
# 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 = 'EN.URB.MCTY'

# 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 <- 'EN.URB.MCTY'

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