def _decade_mortality_table(year,
url_template='https://www.ssa.gov/oact/NOTES/as120/LifeTables_Tbl_7_{}.html'):
assert int(year) % 10 == 0
url = url_template.format(year)
soup = BeautifulSoup(urlopen(url).read(), 'lxml')
table = soup.find('table', border=1)
rows = []
for row in table.find_all('tr'):
row_datum = [cell.text.strip() for cell in row.find_all('td')]
if len(row_datum) == 15 and row_datum[0] != '':
rows.append({
'year_of_birth': int(year),
'age': int(row_datum[0]),
'm_prob_survive_that_year': 1 - float(row_datum[1]),
'f_prob_survive_that_year': 1 - float(row_datum[9]),
})
df = pd.DataFrame(rows).sort_values(by='age')
for sex in 'mf':
df[sex + '_prob_alive'] = np.cumprod(df[sex + '_prob_survive_that_year']).astype(np.float64)
df['as_of_year'] = df['year_of_birth'] + df['age']
return df[['year_of_birth', 'as_of_year', 'm_prob_alive', 'f_prob_alive']]
评论列表
文章目录