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 pdimport dataframe_image as dfi# Load the medal standings and world datamedal_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 datasetsmerged_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)
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 floatmerged_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**9merged_data[population_str] = merged_data['Population'].replace({',': ''}, regex=True).astype(float) /10**6merged_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] *1000merged_data[medals_per_capita_gdp] = merged_data[medals_str] / merged_data[per_capita_gdp] *1000merged_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 inspectreturn final_table