text_html_output_1.png
import matplotlib.pyplot as plt

import numpy as np

import os

import pandas as pd

np.random.seed(2019)
os.environ['PYTHONHASHSEED'] = '2019'
plt.style.use('seaborn-ticks')
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
plt.rcParams['font.size'] = 11.0
plt.rcParams['figure.figsize'] = (9, 6)
pd.set_option('display.max_colwidth', 1000)

population_raw = pd.read_csv('/kaggle/input/covid19-global-forecasting-locations-population/locations_population.csv')

pd.DataFrame(population_raw.isnull().sum()).T

df = population_raw.copy()
df = df.rename({'Province.State': 'Province', 'Country.Region': 'Country'}, axis=1)
cols = ['Country', 'Province', 'Population']
df = df.loc[:, cols].fillna('-')
df.loc[df['Country'] == df['Province'], 'Province'] = '-'
_total_df = df.loc[df['Province'] != '-', :].groupby('Country').sum()
_total_df = _total_df.reset_index().assign(Province='-')
df = pd.concat([df, _total_df], axis=0, sort=True)
df = df.drop_duplicates(subset=['Country', 'Province'], keep='first')
global_value = df.loc[df['Province'] == '-', 'Population'].sum()
df = df.append(pd.Series(['Global', '-', global_value], index=cols), ignore_index=True)
df = df.sort_values('Population', ascending=False).reset_index(drop=True)
df = df.loc[:, cols]
population_df = df.copy()

df = population_df.loc[population_df['Province'] == '-', :]
population_dict = df.set_index('Country').to_dict()['Population']
population_dict

pyramid_csv_list = list()
for dirname, _, filenames in os.walk('/kaggle/input/population-pyramid-2019/'):
    for filename in filenames:
        name = os.path.join(dirname, filename)
        df = pd.read_csv(name)
        df['Country'], df['Year'], _ = filename.replace('.', '-').split('-')
        pyramid_csv_list.append(df)
pyramid_raw = pd.concat(pyramid_csv_list, sort=True)

df = pyramid_raw.copy()
df['Country'] = df['Country'].replace({'United States of America': 'US', 'United Kingdom': 'UK'})
_male = [349432556, 342927576, 331497486, 316642222, 308286775, 306059387, 309236984, 276447037, 249389688, 241232876, 222609691, 192215395, 157180267, 128939392, 87185982, 54754941, 33648953, 15756942, 5327866, 1077791, 124144]
_female = [328509234, 321511867, 309769906, 295553758, 289100903, 288632766, 296293748, 268371754, 244399176, 238133281, 223162982, 195633743, 164961323, 140704320, 101491347, 69026831, 48281201, 26429329, 11352182, 3055845, 449279]
_df = pd.DataFrame({'Age': df['Age'].unique(), 'Country': 'Global', 'F': _female, 'M': _male, 'Year': 2019})
df = pd.concat([df, _df], axis=0, ignore_index=True, sort=True)
_male = [307116, 304759, 296771, 270840, 291723, 376952, 343311, 315086, 312017, 336452, 342117, 306949, 279609, 265511, 273061, 195029, 113166, 61775, 26170, 6768, 415]
_female = [290553, 288817, 280944, 257677, 274760, 361526, 330153, 300752, 301288, 327453, 331458, 300084, 280009, 272149, 286879, 212480, 143654, 97633, 52624, 18130, 1771]
_df = pd.DataFrame({'Age': df['Age'].unique(), 'Country': 'Sweden', 'F': _female, 'M': _male, 'Year': 2019})
df = pd.concat([df, _df], axis=0, ignore_index=True, sort=True)
_male = [5534962, 5820604, 5538414, 5383822, 5149849, 4710777, 4061897, 3581091, 3237426, 2832825, 2482953, 2015857, 1556935, 1082875, 668107, 364200, 199400, 73508, 17327, 3035, 208]
_female = [5240508, 5541514, 5273495, 5029137, 4896316, 4589506, 3982681, 3544279, 3191565, 2825286, 2521463, 2112380, 1714689, 1285782, 895866, 567282, 360751, 155294, 57969, 13376, 1411]
_df = pd.DataFrame({'Age': df['Age'].unique(), 'Country': 'Philippines', 'F': _female, 'M': _male, 'Year': 2019})
df = pd.concat([df, _df], axis=0, ignore_index=True, sort=True)
df['Population'] = df['F'] + df['M']
df = df.pivot_table(index='Age', columns=['Country'], values='Population', aggfunc='last')
df = df.astype(np.int64).reset_index().rename({'Age': 'Age_bin'}, axis=1)
series = df['Age_bin'].str.replace('+', '-122')
df[['Age_first', 'Age_last']] = series.str.split('-', expand=True).astype(np.int64)
df = df.drop('Age_bin', axis=1)
series = df['Age_last']
df = df.apply(lambda x: x[:-2] / (x[-1] - x[-2] + 1), axis=1)
df['Age'] = series
df = pd.merge(df, pd.DataFrame({'Age': np.arange(0, 123, 1)}), on='Age', how='right', sort=True)
df = df.fillna(method='bfill').astype(np.int64)
df = df.set_index('Age')
pyramid_df = df.copy()
pyramid_df