|
| 1 | +import pandas as pd |
| 2 | +import re |
| 3 | + |
| 4 | +international_bestsellers = pd.read_csv("datasets/international_bestsellers.csv") |
| 5 | +international_bestsellers = international_bestsellers.drop_duplicates(subset=["title"]) |
| 6 | + |
| 7 | +all_countries = pd.read_csv("datasets/all.csv") |
| 8 | +population = pd.read_csv("datasets/population.csv") |
| 9 | +all_countries = all_countries.merge(population, left_on="alpha-3", right_on="Country Code") |
| 10 | + |
| 11 | +year_cols = [str(y) for y in range(1960, 2025)] |
| 12 | +all_countries = all_countries[["name", "country-code", "alpha-2"] + year_cols] |
| 13 | + |
| 14 | +# Melt population into long format: one row per (country, year) |
| 15 | +pop_long = all_countries.melt( |
| 16 | + id_vars=["name", "country-code", "alpha-2"], |
| 17 | + value_vars=year_cols, |
| 18 | + var_name="year", |
| 19 | + value_name="population" |
| 20 | +) |
| 21 | +pop_long["year"] = pop_long["year"].astype(int) |
| 22 | +pop_long["population"] = pd.to_numeric(pop_long["population"], errors="coerce") |
| 23 | + |
| 24 | +aliases = { |
| 25 | + "Vietnam": "Viet Nam", |
| 26 | + "Turkey": "Türkiye", |
| 27 | + "South Korea": "Korea, Republic of", |
| 28 | + "Morroco": "Morocco", |
| 29 | + "Ivory Coast": "Côte d'Ivoire", |
| 30 | + "Czech Republic": "Czechia", |
| 31 | + "Scotland": "United Kingdom of Great Britain and Northern Ireland", |
| 32 | + "Algiers": "Algeria", |
| 33 | + "Taiwan": "Taiwan, Province of China" |
| 34 | +} |
| 35 | + |
| 36 | +def find_match(nationality, country_names): |
| 37 | + nationality_lower = nationality.lower().strip() |
| 38 | + for name in country_names: |
| 39 | + if nationality_lower in name.lower() or name.lower() in nationality_lower: |
| 40 | + return name |
| 41 | + if nationality.strip() in aliases: |
| 42 | + return aliases[nationality.strip()] |
| 43 | + return None |
| 44 | + |
| 45 | +nationalities = international_bestsellers[["author", "nationality", "date"]].copy() |
| 46 | +nationalities["nationality"] = ( |
| 47 | + nationalities["nationality"] |
| 48 | + .apply(lambda n: re.findall(r"[\w'\s]+", str(n))) |
| 49 | +) |
| 50 | +nationalities = nationalities.explode("nationality") |
| 51 | +nationalities["nationality"] = nationalities["nationality"].str.strip() |
| 52 | +nationalities = nationalities[nationalities["nationality"].str.len() > 0] |
| 53 | +nationalities["year"] = pd.to_datetime(nationalities["date"], errors="coerce").dt.year |
| 54 | + |
| 55 | +grouped = ( |
| 56 | + nationalities |
| 57 | + .groupby(["nationality", "year"]) |
| 58 | + .size() |
| 59 | + .reset_index(name="counts") |
| 60 | +) |
| 61 | + |
| 62 | +all_names = all_countries["name"].tolist() |
| 63 | +grouped["matched_name"] = grouped["nationality"].apply( |
| 64 | + lambda n: find_match(n, all_names) |
| 65 | +) |
| 66 | + |
| 67 | +# Merge country metadata (without population columns — those are in pop_long) |
| 68 | +country_meta = all_countries[["name", "country-code", "alpha-2"]].copy() |
| 69 | +merged = pd.merge( |
| 70 | + grouped, |
| 71 | + country_meta, |
| 72 | + left_on="matched_name", |
| 73 | + right_on="name", |
| 74 | + how="left" |
| 75 | +) |
| 76 | +merged = merged.rename(columns={"country-code": "ID", "alpha-2":"alpha2"}) |
| 77 | + |
| 78 | +merged_agg = ( |
| 79 | + merged |
| 80 | + .groupby(["ID", "alpha2", "year"], dropna=False) |
| 81 | + .agg( |
| 82 | + nationality=("nationality", "first"), |
| 83 | + counts=("counts", "sum"), |
| 84 | + ) |
| 85 | + .reset_index() |
| 86 | +) |
| 87 | + |
| 88 | +merged_agg = merged_agg[merged_agg["ID"].notna()] |
| 89 | +merged_agg["ID"] = merged_agg["ID"].astype(int) |
| 90 | + |
| 91 | +# Join population for the matching (country, year) |
| 92 | +merged_agg = pd.merge( |
| 93 | + merged_agg, |
| 94 | + pop_long[["country-code", "year", "population"]], |
| 95 | + left_on=["ID", "year"], |
| 96 | + right_on=["country-code", "year"], |
| 97 | + how="left" |
| 98 | +) |
| 99 | + |
| 100 | +# Scale: books per million inhabitants |
| 101 | +merged_agg["counts_raw"] = merged_agg["counts"] |
| 102 | +merged_agg["counts"] = ( |
| 103 | + merged_agg["counts"] / merged_agg["population"] * 1_000_000 |
| 104 | +).round(4) |
| 105 | + |
| 106 | +# Warn about missing population data |
| 107 | +missing_pop = merged_agg[merged_agg["population"].isna()] |
| 108 | +if not missing_pop.empty: |
| 109 | + print("Missing population data for:") |
| 110 | + print(missing_pop[["nationality", "year"]].drop_duplicates().to_string()) |
| 111 | + |
| 112 | +merged_agg = merged_agg.drop(columns=["country-code", "population"]) |
| 113 | +merged_agg.to_csv("datasets/nat_date.csv", index=False) |
| 114 | + |
| 115 | +print("Unmatched nationalities:") |
| 116 | +print(grouped[grouped["matched_name"].isna()]["nationality"].unique()) |
0 commit comments