Paris Olympics 2024 Medal standings

Data sources for medals (https://lnkd.in/gHCTqBCD) and country stats for 2023 (https://lnkd.in/ggpCHi_i).

This is not much different then Tokyo 2020:

# !pip install pandas dataframe-image
import pandas as pd
import dataframe_image as dfi

# Load the medal standings and world data
medal_standings = pd.read_csv('paris_2024_olympics_full_medal_standings_final.csv')
world_data = pd.read_csv('world-data-2023.csv')

# Assuming 'Country' is the column to join on in both datasets
merged_data = pd.merge(medal_standings, world_data, on='Country', how='left')

merged_data = merged_data[['Country', 'Total', 'Population', 'GDP']]

merged_data.to_csv('merged_data.csv', index=False)
merged_data = pd.read_csv('merged_data_adjusted.csv')
population_str = 'Population (in M)'
GDP_str =  'GDP (in $B)'


medals_per_million_people_str = 'Total medals per 1M capita'
medals_per_gdp = 'Total medals per $1B GDP'
medals_per_capita_gdp = 'Total medals per capita GDP'
per_capita_gdp = 'Per capita GDP (in $)'

# Remove dollar signs and commas from the 'GDP' column, then convert to float

merged_data.rename(columns={'Total': 'Total medals'}, inplace=True) 

medals_str = 'Total medals'

merged_data[GDP_str] = merged_data['GDP'].replace({r'\$': '', ',': ''}, regex=True).astype(float) / 10**9
merged_data[population_str] = merged_data['Population'].replace({',': ''}, regex=True).astype(float) / 10**6

merged_data[medals_per_million_people_str] = merged_data[medals_str] / merged_data[population_str]
merged_data[medals_per_gdp] = merged_data[medals_str] / merged_data[GDP_str]
merged_data[per_capita_gdp] = merged_data[GDP_str] / merged_data[population_str] * 1000
merged_data[medals_per_capita_gdp] = merged_data[medals_str] / merged_data[per_capita_gdp] * 1000

merged_data.to_csv('merged_data_w_per_capita_GDP.csv', index=False)
def sort_by_column(column_name, column_name2=None, column_name3=None):

    # Sort the DataFrame by the 'Medals per 1M capita' in descending order
    sorted_data = merged_data.sort_values(by=column_name, ascending=False)
    

    final_table = sorted_data[['Country', 'Total medals'] + ([column_name2] if column_name2 else []) + ([column_name3] if column_name3 else [])].reset_index(drop=True).tail(10)
    
    final_table = final_table.round(2)
    
    # Create a new DataFrame with only 'Country' and 'Medals per 1M capita'
    final_table.index = final_table.index + 1
    
            # Style the DataFrame to add borders
    styled_final_table = final_table.style.set_table_styles(
    [{'selector': 'th',
      'props': [('border', '2px solid black')]},
     {'selector': 'td',
      'props': [('border', '1px solid black')]},
     {'selector': 'table',
      'props': [('border', '10px solid black')]}]
).format(precision=2)

    # Save the table as a PNG file
    dfi.export(styled_final_table, f'opposite_final_table_{column_name}.png')

    # Optionally, you can print the top 10 rows to inspect
    return final_table
merged_data.head(50)
Country Total medals Population GDP GDP (in $B) Population (in M) Total medals per 1M capita Total medals per $1B GDP Per capita GDP (in $) Total medals per capita GDP
0 United States 126 328,239,523 $21,427,700,000,000 21427.700000 328.239523 0.383866 0.005880 65280.682241 1.930127
1 China 91 1,397,715,000 $19,910,000,000,000 19910.000000 1397.715000 0.065106 0.004571 14244.677921 6.388351
2 Japan 45 126,226,568 $5,081,769,542,380 5081.769542 126.226568 0.356502 0.008855 40259.112031 1.117759
3 Australia 53 25,766,605 $1,392,680,589,329 1392.680589 25.766605 2.056926 0.038056 54049.828812 0.980577
4 France 64 67,059,887 $2,715,518,274,227 2715.518274 67.059887 0.954371 0.023568 40493.928572 1.580484
5 Netherlands 34 17,332,850 $909,070,395,161 909.070395 17.332850 1.961593 0.037401 52447.831439 0.648263
6 United Kingdom 65 66,834,405 $2,827,113,184,696 2827.113185 66.834405 0.972553 0.022992 42300.267126 1.536633
7 South Korea 32 51,709,098 $2,029,000,000,000 2029.000000 51.709098 0.618847 0.015771 39238.742861 0.815521
8 Italy 40 60,297,396 $2,001,244,392,042 2001.244392 60.297396 0.663379 0.019988 33189.565799 1.205198
9 Germany 33 83,132,799 $3,845,630,030,824 3845.630031 83.132799 0.396955 0.008581 46258.878290 0.713377
10 New Zealand 20 4,841,000 $206,928,765,544 206.928766 4.841000 4.131378 0.096652 42745.045558 0.467890
11 Canada 27 36,991,981 $1,736,425,629,520 1736.425630 36.991981 0.729888 0.015549 46940.595842 0.575195
12 Uzbekistan 13 33,580,650 $57,921,286,440 57.921286 33.580650 0.387128 0.224443 1724.841134 7.536926
13 Hungary 19 9,769,949 $160,967,157,504 160.967158 9.769949 1.944739 0.118037 16475.741839 1.153211
14 Spain 18 47,076,781 $1,394,116,310,769 1394.116311 47.076781 0.382354 0.012911 29613.671138 0.607827
15 Sweden 11 10,285,453 $530,832,908,738 530.832909 10.285453 1.069472 0.020722 51610.066055 0.213137
16 Kenya 11 52,573,973 $95,503,088,538 95.503089 52.573973 0.209229 0.115180 1816.546916 6.055445
17 Norway 8 5,347,896 $403,336,363,636 403.336364 5.347896 1.495915 0.019835 75419.634869 0.106073
18 Republic of Ireland 7 5,007,069 $388,698,711,348 388.698711 5.007069 1.398023 0.018009 77629.988991 0.090171
19 Brazil 20 212,559,417 $1,839,758,040,766 1839.758041 212.559417 0.094091 0.010871 8655.264804 2.310732
20 Iran 12 82,913,906 $445,345,282,123 445.345282 82.913906 0.144728 0.026945 5371.177208 2.234147
21 Ukraine 12 44,385,155 $153,781,069,118 153.781069 44.385155 0.270361 0.078033 3464.696003 3.463507
22 Romania 9 19,356,544 $250,077,444,017 250.077444 19.356544 0.464959 0.035989 12919.529644 0.696620
23 Georgia 7 3,720,382 $17,743,195,770 17.743196 3.720382 1.881527 0.394517 4769.186543 1.467756
24 Belgium 10 11,484,055 $529,606,710,418 529.606710 11.484055 0.870773 0.018882 46116.699234 0.216841
25 Bulgaria 7 6,975,761 $86,000,000,000 86.000000 6.975761 1.003475 0.081395 12328.404026 0.567795
26 Serbia 5 6,944,975 $51,409,167,351 51.409167 6.944975 0.719945 0.097259 7402.354559 0.675461
27 Czech Republic 5 10,669,709 $246,489,245,495 246.489245 10.669709 0.468616 0.020285 23101.777705 0.216434
28 Denmark 9 5,818,553 $348,078,018,464 348.078018 5.818553 1.546776 0.025856 59822.092961 0.150446
29 Azerbaijan 7 10,023,318 $39,207,000,000 39.207000 10.023318 0.698372 0.178540 3911.578980 1.789559
30 Croatia 7 4,067,500 $60,415,553,039 60.415553 4.067500 1.720959 0.115864 14853.239837 0.471278
31 Cuba 9 11,333,483 $100,023,000,000 100.023000 11.333483 0.794107 0.089979 8825.442276 1.019779
32 Bahrain 4 1,501,635 $38,574,069,149 38.574069 1.501635 2.663763 0.103697 25688.046129 0.155714
33 Slovenia 3 2,087,946 $53,742,159,517 53.742160 2.087946 1.436819 0.055822 25739.247814 0.116554
34 Chinese Taipei 7 23,196,178 $791,610,000,000 791.610000 23.196178 0.301774 0.008843 34126.742776 0.205118
35 Austria 5 8,877,067 $446,314,739,528 446.314740 8.877067 0.563249 0.011203 50277.275087 0.099449
36 Hong Kong 4 7,346,000 $359,800,000,000 359.800000 7.346000 0.544514 0.011117 48979.036210 0.081668
37 Philippines 4 108,116,615 $376,795,508,680 376.795509 108.116615 0.036997 0.010616 3485.084218 1.147748
38 Algeria 3 43,053,054 $169,988,236,398 169.988236 43.053054 0.069681 0.017648 3948.343279 0.759812
39 Indonesia 3 270,203,917 $1,119,190,780,753 1119.190781 270.203917 0.011103 0.002681 4142.022785 0.724284
40 Israel 7 9,053,300 $395,098,666,122 395.098666 9.053300 0.773199 0.017717 43641.397736 0.160398
41 Poland 10 37,970,874 $592,164,400,688 592.164401 37.970874 0.263360 0.016887 15595.227033 0.641222
42 Kazakhstan 7 18,513,930 $180,161,741,180 180.161741 18.513930 0.378094 0.038854 9731.145207 0.719340
43 Jamaica 6 2,948,279 $16,458,071,068 16.458071 2.948279 2.035086 0.364563 5582.263778 1.074833
44 South Africa 6 58,558,270 $351,431,649,241 351.431649 58.558270 0.102462 0.017073 6001.400814 0.999767
45 Thailand 6 69,625,582 $543,649,976,166 543.649976 69.625582 0.086175 0.011037 7808.192916 0.768424
46 Ethiopia 4 112,078,730 $96,107,662,398 96.107662 112.078730 0.035689 0.041620 857.501351 4.664716
47 Switzerland 8 8,574,832 $703,082,435,360 703.082435 8.574832 0.932963 0.011378 81993.727149 0.097568
48 Ecuador 5 17,373,662 $107,435,665,000 107.435665 17.373662 0.287792 0.046539 6183.823825 0.808561
49 Portugal 4 10,269,417 $237,686,075,635 237.686076 10.269417 0.389506 0.016829 23145.040817 0.172823
sort_by_column(column_name=GDP_str)
Country Total medals
81 Armenia 4
82 Moldova 4
83 Kosovo 2
84 Kyrgyzstan 6
85 Tajikistan 3
86 Fiji 1
87 Saint Lucia 2
88 Cape Verde 1
89 Grenada 2
90 Dominica 1
sort_by_column(column_name=medals_per_million_people_str, column_name2=population_str)
Country Total medals Population (in M)
81 Uganda 2 44.27
82 Mexico 5 126.01
83 Ivory Coast 1 25.72
84 Philippines 4 108.12
85 Ethiopia 4 112.08
86 Peru 1 32.51
87 Egypt 3 100.39
88 Indonesia 3 270.20
89 Pakistan 1 216.57
90 India 6 1366.42
sort_by_column(column_name=medals_per_gdp, column_name2=GDP_str)
Country Total medals GDP (in $B)
81 United States 126 21427.70
82 Malaysia 2 364.70
83 Qatar 1 183.47
84 China 91 19910.00
85 Peru 1 226.85
86 Mexico 5 1258.29
87 Pakistan 1 304.40
88 Singapore 1 372.06
89 Indonesia 3 1119.19
90 India 6 2611.00
sort_by_column(column_name=medals_per_capita_gdp, column_name2=per_capita_gdp, column_name3=medals_per_capita_gdp)
Country Total medals Per capita GDP (in $) Total medals per capita GDP
81 Austria 5 50277.28 0.10
82 Switzerland 8 81993.73 0.10
83 Republic of Ireland 7 77629.99 0.09
84 Hong Kong 4 48979.04 0.08
85 Panama 1 15731.02 0.06
86 Puerto Rico 2 35195.53 0.06
87 Slovakia 1 19329.10 0.05
88 Cyprus 1 20494.88 0.05
89 Qatar 1 64781.73 0.02
90 Singapore 1 65233.28 0.02