Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Line graph of ONS weekly death figures
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"ons_file_location = 'https://www.ons.gov.uk/file?uri=%2fpeoplepopulationandcommunity%2fbirthsdeathsandmarriages%2fdeaths%2fdatasets%2fweeklyprovisionalfiguresondeathsregisteredinenglandandwales%2f2020/publishedweek172020.xlsx'\n",
"ons_file = requests.get(ons_file_location)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_excel(ons_file.content, 'Weekly figures 2020')\n",
"df.drop(df.index[range(10, 115)], axis=0, inplace=True)\n",
"df.drop([0, 1, 2, 5, 6, 8], axis=0, inplace=True)\n",
"df.drop(df.columns[[0, 1]], axis=1, inplace=True)\n",
"df.drop(df.columns[range(17, 53)], axis=1, inplace=True)\n",
"\n",
"df.columns = range(1, len(df.columns) + 1)\n",
"deaths_average = df.iloc[3]\n",
"deaths_2020 = df.iloc[2]\n",
"print('Deaths per week, 2020:')\n",
"print(deaths_2020)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"plt.ylim(0, 25000)\n",
"plt.ylabel('Total Deaths')\n",
"plt.xlabel('Week of Year')\n",
"plt.title('Deaths per Week, England and Wales')\n",
"plt.plot(deaths_2020, label='2020')\n",
"plt.plot(deaths_average, label='2015 - 2019 average')\n",
"plt.legend()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.drop(df.columns[range(11)], axis=1, inplace=True)\n",
"w12_plus_deaths_average = df.iloc[3]\n",
"w12_plus_deaths_2020 = df.iloc[2]\n",
"print('Excess deaths, week 12 to week 17 = ' + str(w12_plus_deaths_2020.sum() - w12_plus_deaths_average.sum()))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.