Population in largest city

Source: worldbank.org, 19.12.2024

Year: 2023

Flag Country Value Value change, % Rank
Afghanistan Afghanistan 4,588,666 +2.93% 42
Angola Angola 9,292,336 +3.8% 25
Albania Albania 520,059 +1.66% 144
United Arab Emirates United Arab Emirates 3,007,583 +1.46% 63
Argentina Argentina 15,490,415 +0.784% 13
Armenia Armenia 1,094,813 +0.255% 121
Australia Australia 5,235,407 +1.64% 41
Austria Austria 1,975,271 +0.778% 86
Azerbaijan Azerbaijan 2,432,304 +1.3% 75
Burundi Burundi 1,206,767 +5.93% 116
Belgium Belgium 2,121,992 +0.586% 80
Benin Benin 1,252,890 +5.4% 114
Burkina Faso Burkina Faso 3,203,923 +4.85% 56
Bangladesh Bangladesh 23,209,616 +3.25% 4
Bulgaria Bulgaria 1,288,114 +0.103% 111
Bahrain Bahrain 708,882 +2.95% 133
Bosnia & Herzegovina Bosnia & Herzegovina 345,549 +0.31% 153
Belarus Belarus 2,057,257 +0.424% 82
Bolivia Bolivia 1,935,619 +1.46% 87
Brazil Brazil 22,619,736 +0.847% 5
Central African Republic Central African Republic 958,335 +2.7% 127
Canada Canada 6,371,958 +0.934% 33
Switzerland Switzerland 1,431,538 +0.839% 105
Chile Chile 6,903,392 +0.677% 31
China China 29,210,808 +2.43% 3
Côte d’Ivoire Côte d’Ivoire 5,686,350 +3.09% 37
Cameroon Cameroon 4,509,287 +3.98% 43
Congo - Kinshasa Congo - Kinshasa 16,315,534 +4.4% 10
Congo - Brazzaville Congo - Brazzaville 2,637,733 +3.33% 69
Colombia Colombia 11,507,960 +1.44% 16
Costa Rica Costa Rica 1,461,989 +1.43% 103
Cuba Cuba 2,148,930 +0.151% 79
Czechia Czechia 1,323,339 +0.399% 109
Germany Germany 3,573,938 +0.0893% 50
Djibouti Djibouti 599,504 +1.36% 140
Denmark Denmark 1,381,005 +0.794% 107
Dominican Republic Dominican Republic 3,523,890 +1.92% 51
Algeria Algeria 2,901,810 +1.68% 68
Ecuador Ecuador 3,142,466 +1.62% 60
Egypt Egypt 22,183,201 +1.99% 7
Eritrea Eritrea 1,072,666 +3.65% 125
Spain Spain 6,751,374 +0.563% 32
Estonia Estonia 454,245 +0.547% 150
Ethiopia Ethiopia 5,460,591 +4.45% 39
Finland Finland 1,337,786 +0.755% 108
France France 11,208,440 +0.594% 18
Gabon Gabon 869,773 +1.51% 129
United Kingdom United Kingdom 9,648,110 +1.13% 22
Georgia Georgia 1,082,245 +0.175% 124
Ghana Ghana 3,768,239 +3.8% 49
Guinea Guinea 2,110,937 +3.05% 81
Gambia Gambia 481,472 +2.54% 146
Guinea-Bissau Guinea-Bissau 664,446 +3.41% 136
Equatorial Guinea Equatorial Guinea 474,467 +4.29% 148
Greece Greece 3,154,463 +0.0216% 59
Guatemala Guatemala 3,095,099 +1.93% 61
Hong Kong SAR China Hong Kong SAR China 7,684,801 +0.544% 29
Honduras Honduras 1,568,025 +2.71% 101
Croatia Croatia 684,114 -0.0205% 134
Haiti Haiti 2,987,455 +2.48% 65
Hungary Hungary 1,778,052 +0.16% 90
Indonesia Indonesia 11,248,839 +1.57% 17
India India 32,941,309 +2.73% 2
Ireland Ireland 1,270,172 +1.13% 113
Iran Iran 9,499,781 +1.26% 23
Iraq Iraq 7,711,305 +2.65% 28
Israel Israel 4,420,855 +1.78% 44
Italy Italy 4,315,671 +0.414% 45
Jamaica Jamaica 597,124 +0.436% 141
Jordan Jordan 2,232,240 +1.03% 78
Japan Japan 37,194,105 -0.214% 1
Kazakhstan Kazakhstan 1,987,301 +1.49% 83
Kenya Kenya 5,325,160 +4.03% 40
Kyrgyzstan Kyrgyzstan 1,104,742 +2.08% 120
Cambodia Cambodia 2,281,198 +3.15% 77
South Korea South Korea 9,988,049 +0.124% 21
Kuwait Kuwait 3,297,759 +1.83% 54
Laos Laos 721,199 +2.09% 132
Lebanon Lebanon 2,421,354 -0.485% 76
Liberia Liberia 1,678,020 +3.42% 94
Libya Libya 1,183,292 +0.635% 117
Sri Lanka Sri Lanka 632,543 +1.1% 137
Lithuania Lithuania 541,275 +0.0925% 143
Latvia Latvia 621,369 -0.569% 138
Macao SAR China Macao SAR China 681,876 +1.49% 135
Morocco Morocco 3,892,837 +1.37% 46
Moldova Moldova 487,707 -0.596% 145
Madagascar Madagascar 3,872,264 +4.66% 47
Mexico Mexico 22,281,442 +0.889% 6
North Macedonia North Macedonia 611,107 +0.843% 139
Mali Mali 2,929,373 +3.99% 67
Myanmar (Burma) Myanmar (Burma) 5,610,241 +1.74% 38
Mongolia Mongolia 1,672,627 +1.71% 96
Mozambique Mozambique 1,852,405 +3.09% 88
Mauritania Mauritania 1,491,958 +4.22% 102
Malawi Malawi 1,276,316 +4.42% 112
Malaysia Malaysia 8,621,724 +2.4% 26
Namibia Namibia 477,267 +3.5% 147
Niger Niger 1,437,233 +3.85% 104
Nigeria Nigeria 15,945,912 +3.63% 11
Nicaragua Nicaragua 1,094,510 +1.05% 122
Netherlands Netherlands 1,174,025 +0.697% 118
Norway Norway 1,085,992 +1.39% 123
Nepal Nepal 1,571,010 +3.28% 100
New Zealand New Zealand 1,673,220 +1.26% 95
Oman Oman 1,650,319 +1.71% 97
Pakistan Pakistan 17,236,230 +2.35% 9
Panama Panama 1,976,866 +2.01% 85
Peru Peru 11,204,382 +1.45% 19
Philippines Philippines 14,667,089 +1.81% 14
Papua New Guinea Papua New Guinea 409,831 +2.44% 152
Poland Poland 1,797,516 +0.166% 89
Puerto Rico Puerto Rico 2,439,564 -0.121% 74
North Korea North Korea 3,157,538 +0.798% 58
Portugal Portugal 3,000,536 +0.481% 64
Paraguay Paraguay 3,510,511 +1.69% 52
Palestinian Territories Palestinian Territories 778,187 +2.92% 131
Qatar Qatar 798,382 +2.44% 130
Romania Romania 1,776,385 -0.499% 91
Russia Russia 12,680,389 +0.313% 15
Rwanda Rwanda 1,247,551 +3.25% 115
Saudi Arabia Saudi Arabia 7,682,430 +1.91% 30
Sudan Sudan 6,344,348 +2.99% 34
Senegal Senegal 3,429,536 +3.11% 53
Singapore Singapore 6,080,859 +0.684% 36
Sierra Leone Sierra Leone 1,309,168 +2.91% 110
El Salvador El Salvador 1,116,052 +0.484% 119
Somalia Somalia 2,610,483 +4.53% 70
Serbia Serbia 1,408,144 +0.21% 106
South Sudan South Sudan 459,342 +4.4% 149
Slovakia Slovakia 440,787 +0.391% 151
Sweden Sweden 1,700,066 +1.25% 93
Syria Syria 2,584,771 +3.28% 72
Chad Chad 1,592,324 +3.9% 98
Togo Togo 1,981,615 +2.91% 84
Thailand Thailand 11,069,982 +1.56% 20
Tajikistan Tajikistan 986,899 +2.57% 126
Turkmenistan Turkmenistan 902,353 +2.15% 128
Trinidad & Tobago Trinidad & Tobago 545,045 +0.0856% 142
Tunisia Tunisia 2,475,446 +1.48% 73
Turkey Turkey 15,847,768 +1.35% 12
Tanzania Tanzania 7,775,865 +5.01% 27
Uganda Uganda 3,846,102 +5.32% 48
Ukraine Ukraine 3,016,789 +0.219% 62
Uruguay Uruguay 1,774,396 +0.405% 92
United States United States 18,937,269 +0.375% 8
Uzbekistan Uzbekistan 2,603,243 +1.15% 71
Venezuela Venezuela 2,972,145 +0.519% 66
Vietnam Vietnam 9,320,866 +2.68% 24
Yemen Yemen 3,292,497 +3.48% 55
South Africa South Africa 6,198,016 +2.19% 35
Zambia Zambia 3,181,250 +4.58% 57
Zimbabwe Zimbabwe 1,578,128 +1.31% 99

                    
# 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.LCTY'

# 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.LCTY'

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