<jupyter_start><jupyter_text>Per Capita COVID-19 cases worldwide> GIS plot of per capita cases of COVID-19 all over the world- toc: false- comments: true- image: images/world_per_capita_cases.png- hide: false- search_exclude: false- categories: [geospatial, interactive, chloropleth, NYT]- author: <NAME>- badges: true Today we will make the Per Capita covid cases worldwide from the article [Coronavirus Map: Tracking the Global Outbreak](https://www.nytimes.com/interactive/2020/world/coronavirus-maps.html) that looks like the following -![per capita world cases](images/world_per_capita_cases_2.png)<jupyter_code>import altair as alt
import pandas as pd
import geopandas as gpd
alt.renderers.set_embed_options(actions=False)<jupyter_output><empty_output><jupyter_text>We will use the JHU CSSE Dataset for the cases as well as the population. For the map we will use the shapefiles from [Natural Earth](https://www.naturalearthdata.com/).<jupyter_code>population_uri = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/UID_ISO_FIPS_LookUp_Table.csv'
population_data = pd.read_csv(population_uri)

latest_cases_uri = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/10-29-2020.csv'
latest_cases = pd.read_csv(latest_cases_uri)

world_shapefile_uri = "https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip"
world = gpd.read_file(world_shapefile_uri)<jupyter_output><empty_output><jupyter_text>We have to make some changes so that we get things right. For example there's quite a few names to change for the countries because the cases dataset doesn't have identifier informations. Then in the map file we have to merge a few countries and segregate a few based on how JHU CSSE reports their cases.  In the Map- Somalia is combination of Somalia and Somaliland // NYT shows them together(combined)- Greenland is separate from Denmark // latestcases shows them together but NYT shows them separately<jupyter_code>latest_cases.head()
latest_cases[latest_cases['Country_Region'].str.contains('Denmark')]
latest_cases.loc[latest_cases['Province_State']=='Greenland', 'Country_Region'] = "Greenland"

population_data.loc[population_data['Province_State']=='Greenland', 'Country_Region'] = "Greenland"
population_data.loc[population_data['Province_State']=='Greenland', 'Combined_Key'] = "Greenland"
latest_cases = latest_cases.drop(['FIPS', 'Admin2', 'Province_State', 'Last_Update', 'Lat', 'Long_', 'Combined_Key', 'Incidence_Rate', 'Case-Fatality_Ratio'], axis=1)
latest_cases = latest_cases.groupby('Country_Region').aggregate({'Confirmed': 'sum', 'Recovered': 'sum', 'Deaths': 'sum', 'Active': 'sum', })
latest_cases = latest_cases.reset_index()
latest_cases.head()
world = world[~(world['CONTINENT']=='Antarctica')]
world = world[['SOVEREIGNT', 'ADMIN', 'NAME', 'POP_EST', 'POP_YEAR', 'ISO_A3', 'CONTINENT', 'geometry']]
world.head()
alt.Chart(world).mark_geoshape(stroke='white').encode().project('equalEarth')
world[world['NAME'].str.contains('Green')]
somalia = world[world['NAME'].str.contains('Somali')]
somalia = somalia.dissolve(by='CONTINENT').reset_index()
somalia
world=pd.concat([world, somalia])
alt.Chart(somalia).mark_geoshape().encode()<jupyter_output><empty_output><jupyter_text>Making sure that names are same<jupyter_code>world.loc[world['ADMIN'].str.contains('eSwatini'), 'ADMIN'] = 'Eswatini'
world.loc[world['ADMIN'].str.contains('Palestine'), 'ADMIN'] = 'West Bank and Gaza'
world.loc[world['ADMIN'].str.contains('Republic of Serbia'), 'ADMIN'] = 'Serbia'
world.loc[world['ADMIN'].str.contains('United Republic of Tanzania'), 'ADMIN'] = 'Tanzania'
world.loc[world['ADMIN'].str.contains('São Tomé and Principe'), 'ADMIN'] = 'Sao Tome and Principe'
latest_cases.loc[latest_cases['Country_Region']=='Korea, South', 'Country_Region'] = 'South Korea'
latest_cases.loc[latest_cases['Country_Region']=="Cote d'Ivoire", 'Country_Region'] = 'Ivory Coast'
latest_cases.loc[latest_cases['Country_Region']=='Timor-Leste', 'Country_Region'] = 'East Timor'
latest_cases.loc[latest_cases['Country_Region']=='Taiwan*', 'Country_Region'] = 'Taiwan'
latest_cases.loc[latest_cases['Country_Region']=='Burma', 'Country_Region'] = 'Myanmar'
latest_cases.loc[latest_cases['Country_Region']=='US', 'Country_Region'] = 'United States of America'
latest_cases.loc[latest_cases['Country_Region']=='Czech Republic', 'Country_Region'] = 'Czechia'
latest_cases.loc[latest_cases['Country_Region']=='North Macedonia', 'Country_Region'] = 'Macedonia'
latest_cases.loc[latest_cases['Country_Region']=='Bahamas', 'Country_Region'] = 'The Bahamas'
latest_cases.loc[latest_cases['Country_Region']=='Congo (Kinshasa)', 'Country_Region'] = 'Democratic Republic of the Congo'
latest_cases.loc[latest_cases['Country_Region']=='Congo (Brazzaville)', 'Country_Region'] = 'Republic of the Congo'<jupyter_output><empty_output><jupyter_text>We will ignore this for now as they are ships/cruises (NYT does however show them as aggregates of corresponding countries)<jupyter_code>latest_cases[latest_cases['Country_Region'].isin(world['ADMIN']) == False]<jupyter_output><empty_output><jupyter_text>Extracting population data for countries -<jupyter_code>population_data = population_data.drop(['UID', 'code3', 'FIPS', 'Admin2', 'Province_State', 'Lat', 'Long_'], axis=1)
population_data = population_data[population_data['Country_Region'] == population_data['Combined_Key']]
population_data = population_data.reset_index(drop=True)
population_data.head()
population_data.loc[population_data['Country_Region']=='Taiwan*', 'Country_Region'] = 'Taiwan'
population_data.loc[population_data['Country_Region']=='Korea, South', 'Country_Region'] = 'South Korea'
population_data.loc[population_data['Country_Region']=="Cote d'Ivoire", 'Country_Region'] = 'Ivory Coast'
population_data.loc[population_data['Country_Region']=='Timor-Leste', 'Country_Region'] = 'East Timor'
population_data.loc[population_data['Country_Region']=='US', 'Country_Region'] = 'United States of America'
population_data.loc[population_data['Country_Region']=='Czech Republic', 'Country_Region'] = 'Czechia'
population_data.loc[population_data['Country_Region']=='Burma', 'Country_Region'] = 'Myanmar'
population_data.loc[population_data['Country_Region']=='North Macedonia', 'Country_Region'] = 'Macedonia'
population_data.loc[population_data['Country_Region']=='Bahamas', 'Country_Region'] = 'The Bahamas'
population_data.loc[population_data['Country_Region']=='Congo (Kinshasa)', 'Country_Region'] = 'Democratic Republic of the Congo'
population_data.loc[population_data['Country_Region']=='Congo (Brazzaville)', 'Country_Region'] = 'Republic of the Congo'
world.columns = ['SOVEREIGNT', 'Country_Region', 'NAME', 'POP_EST',	'POP_YEAR',	'ISO_A3', 'CONTINENT', 'geometry']
world = world.merge(latest_cases, on='Country_Region', how='left')
world = world.merge(population_data, on='Country_Region', how='left')
world['per_capita'] = world['Confirmed']/world['Population']
world.head()
world['code'] = world['per_capita'].apply(lambda x: 'Less than 1 in 1000' if x <= (1/1000) else 'Less than 1 in 500' if x<= (1/500) else 'Less than 1 in 333' if x<= (1/333) else 'No Cases reported' if pd.isnull(x) else 'Greater than 1 in 333')
world['Share of Population'] = world['Population']/world['Confirmed']
world['Share of Population'] = world['Share of Population'].round()
world['Share of Population'] = world['Share of Population'].apply(lambda x: f"1 in {str(x).split('.')[0]}")<jupyter_output><empty_output><jupyter_text>Making the chart -<jupyter_code>alt.Chart(world).mark_geoshape(stroke='white').transform_filter(alt.datum.Country_Region != 'Antarctica').encode(
    color=alt.Color('code:N', scale=alt.Scale(domain=['No Cases reported', 'Less than 1 in 1000', 'Less than 1 in 500', 'Less than 1 in 333', 'Greater than 1 in 333'], range=['lightgrey', '#f2df91', '#ffae43', '#ff6e0b', '#ce0a05']),legend=alt.Legend(title=None, orient='top', labelBaseline='middle', symbolType='square', columnPadding=20, labelFontSize=15, gridAlign='each', symbolSize=200)),
    tooltip = ['Country_Region', 'Confirmed', 'Share of Population']
).properties(width=1400, height=800).project('equalEarth').configure_view(strokeWidth=0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15592814371257485, 'fraction_numerical': 0.013772455089820359, 'mean_word_length': 9.23406862745098, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 5, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '392470', 'license': 'Apache-2.0', 'n_tokens_mistral': 2912, 'n_tokens_neox': 2693, 'n_words': 557, 'path': '_notebooks/2020-06-28-World Per Capita Cases.ipynb', 'repo_name': 'armsp/covidviz', 'size': '8355'}