{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5479ba3f-8efe-43ce-89d1-586f2d430276",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d8ad68b117e4b741",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Discovery of Handwashing\n",
    "### Supervised by: Siddarth Marwaha (Sid); Email: marwaha@ifn.ing.tu-bs.de\n",
    "\n",
    "Prior to the nineteenth century, hygiene was frowned upon and people did not understand the risk\n",
    "of not washing their hands. Such a behavior caused many deaths, especially in hospitals where most\n",
    "doctors—unlike today—never washed their hands before surgeries.\n",
    "\n",
    "To resolve this issue, in the mid-1800s, the Hungarian physician, Dr. Ignaz Semmelweis, started\n",
    "investigating the reasons behind the deaths in Vienna general hospital and those of women giving birth\n",
    "in particular. After analyzing data from Clinic 1 and Clinic 2, it turned out the proportion of deaths\n",
    "in Clinic 1 is much higher than that in Clinic 2.\n",
    "\n",
    "By investigating, Dr. Semmelweis concluded that many of the medical students who worked at\n",
    "Clinic 1 also spend a lot of time in the autopsy room as a part of their studies. Thus, he realized that\n",
    "dealing with corpses spread harmful bacteria that was transferred to the women giving birth, thus\n",
    "infecting them with the deadly childbed fever - the main reason for the high mortality rates.\n",
    "Analysing the available dataset, you will recreate the steps that someone did 200 years ago but this\n",
    "time using advanced technology.\n",
    "\n",
    "What will You Learn from This Python Project?\n",
    "* Read a dataset and display records from it\n",
    "* Display the relationship between variables through different periods\n",
    "* Perform some calculations to create new variables\n",
    "* Visualize the effect of handwashing on decreasing the number of deaths"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c926362",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e98c740e5cb76fba",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "## Perform the following tasks: Part I\n",
    "\n",
    "* Import the necessary libraries and read the yearly dataset\n",
    "* Explore the dataset via the following properties - shape, size, info\n",
    "* Identify which clinic had more death - you can group the dataset by clinic\n",
    "* Calculate the proportions of deaths\n",
    "* Separate the dataset into two datasets, one for each clinic\n",
    "* Visualize the number of deaths in each clinic every year (bar plot) and identify the most hectic year for each clinic\n",
    "* Plot the proportion of deaths in each clinic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8f76507b",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a350b4e1bd0621ec",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>births</th>\n",
       "      <th>deaths</th>\n",
       "      <th>clinic</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1841</td>\n",
       "      <td>3036</td>\n",
       "      <td>237</td>\n",
       "      <td>clinic 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1842</td>\n",
       "      <td>3287</td>\n",
       "      <td>518</td>\n",
       "      <td>clinic 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1843</td>\n",
       "      <td>3060</td>\n",
       "      <td>274</td>\n",
       "      <td>clinic 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1844</td>\n",
       "      <td>3157</td>\n",
       "      <td>260</td>\n",
       "      <td>clinic 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1845</td>\n",
       "      <td>3492</td>\n",
       "      <td>241</td>\n",
       "      <td>clinic 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1846</td>\n",
       "      <td>4010</td>\n",
       "      <td>459</td>\n",
       "      <td>clinic 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1841</td>\n",
       "      <td>2442</td>\n",
       "      <td>86</td>\n",
       "      <td>clinic 2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1842</td>\n",
       "      <td>2659</td>\n",
       "      <td>202</td>\n",
       "      <td>clinic 2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1843</td>\n",
       "      <td>2739</td>\n",
       "      <td>164</td>\n",
       "      <td>clinic 2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1844</td>\n",
       "      <td>2956</td>\n",
       "      <td>68</td>\n",
       "      <td>clinic 2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1845</td>\n",
       "      <td>3241</td>\n",
       "      <td>66</td>\n",
       "      <td>clinic 2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1846</td>\n",
       "      <td>3754</td>\n",
       "      <td>105</td>\n",
       "      <td>clinic 2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    year  births  deaths    clinic\n",
       "0   1841    3036     237  clinic 1\n",
       "1   1842    3287     518  clinic 1\n",
       "2   1843    3060     274  clinic 1\n",
       "3   1844    3157     260  clinic 1\n",
       "4   1845    3492     241  clinic 1\n",
       "5   1846    4010     459  clinic 1\n",
       "6   1841    2442      86  clinic 2\n",
       "7   1842    2659     202  clinic 2\n",
       "8   1843    2739     164  clinic 2\n",
       "9   1844    2956      68  clinic 2\n",
       "10  1845    3241      66  clinic 2\n",
       "11  1846    3754     105  clinic 2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline \n",
    "\n",
    "#Read the yearly dataset\n",
    "yearly_df = pd.read_csv(\"yearly_deaths_by_clinic.csv\")\n",
    "\n",
    "yearly_df\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9cc28bf5",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-27b4cb1aecd8f676",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12, 4)\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 12 entries, 0 to 11\n",
      "Data columns (total 4 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   year    12 non-null     int64 \n",
      " 1   births  12 non-null     int64 \n",
      " 2   deaths  12 non-null     int64 \n",
      " 3   clinic  12 non-null     object\n",
      "dtypes: int64(3), object(1)\n",
      "memory usage: 512.0+ bytes\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "print(yearly_df.shape)\n",
    "print(yearly_df.info())\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e82dcb2d",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-dc267914fe8b09c6",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "clinic\n",
       "clinic 1    1989\n",
       "clinic 2     691\n",
       "Name: deaths, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "yearly_df.groupby(\"clinic\") [\"deaths\"].sum()\n",
    "# The results shows us the number of births and deaths in 2 clinics from the year 1841 till 1846. \n",
    "# It's obvious that the number of deaths in clinic 1 is higher than that of clinic 2. \n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7ca14e53",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a56098ce57423099",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>births</th>\n",
       "      <th>deaths</th>\n",
       "      <th>clinic</th>\n",
       "      <th>Proportion of Deaths</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1841</td>\n",
       "      <td>3036</td>\n",
       "      <td>237</td>\n",
       "      <td>clinic 1</td>\n",
       "      <td>0.078063</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1842</td>\n",
       "      <td>3287</td>\n",
       "      <td>518</td>\n",
       "      <td>clinic 1</td>\n",
       "      <td>0.157591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1843</td>\n",
       "      <td>3060</td>\n",
       "      <td>274</td>\n",
       "      <td>clinic 1</td>\n",
       "      <td>0.089542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1844</td>\n",
       "      <td>3157</td>\n",
       "      <td>260</td>\n",
       "      <td>clinic 1</td>\n",
       "      <td>0.082357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1845</td>\n",
       "      <td>3492</td>\n",
       "      <td>241</td>\n",
       "      <td>clinic 1</td>\n",
       "      <td>0.069015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1846</td>\n",
       "      <td>4010</td>\n",
       "      <td>459</td>\n",
       "      <td>clinic 1</td>\n",
       "      <td>0.114464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1841</td>\n",
       "      <td>2442</td>\n",
       "      <td>86</td>\n",
       "      <td>clinic 2</td>\n",
       "      <td>0.035217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1842</td>\n",
       "      <td>2659</td>\n",
       "      <td>202</td>\n",
       "      <td>clinic 2</td>\n",
       "      <td>0.075968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1843</td>\n",
       "      <td>2739</td>\n",
       "      <td>164</td>\n",
       "      <td>clinic 2</td>\n",
       "      <td>0.059876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1844</td>\n",
       "      <td>2956</td>\n",
       "      <td>68</td>\n",
       "      <td>clinic 2</td>\n",
       "      <td>0.023004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1845</td>\n",
       "      <td>3241</td>\n",
       "      <td>66</td>\n",
       "      <td>clinic 2</td>\n",
       "      <td>0.020364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1846</td>\n",
       "      <td>3754</td>\n",
       "      <td>105</td>\n",
       "      <td>clinic 2</td>\n",
       "      <td>0.027970</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    year  births  deaths    clinic  Proportion of Deaths\n",
       "0   1841    3036     237  clinic 1              0.078063\n",
       "1   1842    3287     518  clinic 1              0.157591\n",
       "2   1843    3060     274  clinic 1              0.089542\n",
       "3   1844    3157     260  clinic 1              0.082357\n",
       "4   1845    3492     241  clinic 1              0.069015\n",
       "5   1846    4010     459  clinic 1              0.114464\n",
       "6   1841    2442      86  clinic 2              0.035217\n",
       "7   1842    2659     202  clinic 2              0.075968\n",
       "8   1843    2739     164  clinic 2              0.059876\n",
       "9   1844    2956      68  clinic 2              0.023004\n",
       "10  1845    3241      66  clinic 2              0.020364\n",
       "11  1846    3754     105  clinic 2              0.027970"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "#To make the analysis easier, we can calculate the proportion of deaths.\n",
    "yearly_df[\"Proportion of Deaths\"] = yearly_df[\"deaths\"] / yearly_df[\"births\"]\n",
    "yearly_df\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f5507c08",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-18e722b1c6fb0f3b",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "#Separate the dataset into 2 datasets, one for each clinic\n",
    "clinic_1 = yearly_df[yearly_df[\"clinic\"] == \"clinic 1\"]\n",
    "clinic_2 = yearly_df[yearly_df[\"clinic\"] == \"clinic 2\"]\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "87e90bf3",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7b6c5b559ec43db0",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Number of Deaths')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 27744 characters]\n",
      "text/plain": [
       "<Figure size 1000x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Visualize the Number of deaths every year in clinic 1\n",
    "fig,ax = plt.subplots(figsize = (10,4))\n",
    "plt.bar(clinic_1.year, clinic_1.deaths, width= 0.6, color= \"red\")\n",
    "plt.title(\"Clinic 1: Number of Deaths per Year\", fontsize=16)\n",
    "plt.xlabel(\"Year\", fontsize=14)\n",
    "plt.ylabel(\"Number of Deaths\", fontsize=14)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "acb5e257",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-9d755fda49805d5f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Number of Deaths')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 28616 characters]\n",
      "text/plain": [
       "<Figure size 1000x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Visualize the Number of deaths every year in clinic 2\n",
    "fig,ax = plt.subplots(figsize = (10,4))\n",
    "plt.bar(clinic_2.year, clinic_2.deaths, width= 0.6, color= \"green\")\n",
    "plt.title(\"Clinic 2: Number of Deaths per Year\", fontsize=16)\n",
    "plt.xlabel(\"Year\", fontsize=14)\n",
    "plt.ylabel(\"Number of Deaths\", fontsize=14)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4d547ff4",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-1b3899f3cf37fe95",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot: xlabel='year', ylabel='Proportion of Deaths'>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39144 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "ax= clinic_1.plot(x= \"year\", y= \"Proportion of Deaths\", label= \"clinic_1\", color=\"red\")\n",
    "clinic_2.plot(x= \"year\", y= \"Proportion of Deaths\", label= \"clinic_2\", ax=ax, ylabel= \"Proportion of Deaths\", color=\"green\")\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6e8a903",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-636de48362bd4c2a",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "By looking further into why this happened, Dr Semmelweis realized that many medical students who worked at clinic 1 as a part of their study, also spend a lot of time in the autopsy room. Thus, he realized that dealing with corpses spread bacteria that was transferred to the women giving birth and infecting them with the deadly childbed fever. This was the main reason for the high mortality rates. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dc4d4f9",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a2cd98e7481736c8",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Perform the following tasks: Part II\n",
    "\n",
    "* Read the monthly dataset\n",
    "* Explore the monthly dataset via the head, tail, info, and shape properties\n",
    "* Calculate the proportion of deaths per month\n",
    "* Change the data type of \"date\" column from string to datatime\n",
    "* Label the date at which handwashing started to \"start_handwashing\" and split monthly dataset into before and after start_handwashing\n",
    "* Plot the proportions of deaths before and after handwashing\n",
    "* Combine both the two plots into one plot to see the difference clearly\n",
    "* Calculate exactly how much did handwashing decrease the proportion of deaths on average - before and after handwashing\n",
    "* Report the decrease in proportion of deaths due to handwashing "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d17a8da2",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5b4d3c08745cef3c",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Read the monthly dataset\n",
    "monthly_df = pd.read_csv(\"monthly_deaths.csv\")\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e7e0e538",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-bfb1d296c056f333",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         date  births  deaths\n",
      "0  1841-01-01     254      37\n",
      "1  1841-02-01     239      18\n",
      "2  1841-03-01     277      12\n",
      "3  1841-04-01     255       4\n",
      "4  1841-05-01     255       2\n",
      "          date  births  deaths\n",
      "93  1848-11-01     310       9\n",
      "94  1848-12-01     373       5\n",
      "95  1849-01-01     403       9\n",
      "96  1849-02-01     389      12\n",
      "97  1849-03-01     406      20\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 98 entries, 0 to 97\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   date    98 non-null     object\n",
      " 1   births  98 non-null     int64 \n",
      " 2   deaths  98 non-null     int64 \n",
      "dtypes: int64(2), object(1)\n",
      "memory usage: 2.4+ KB\n",
      "None\n",
      "(98, 3)\n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "print(monthly_df.head(5))\n",
    "print(monthly_df.tail(5))\n",
    "print(monthly_df.info())\n",
    "print(monthly_df.shape)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e4621041",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-43974c10b7a44c86",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>births</th>\n",
       "      <th>deaths</th>\n",
       "      <th>Proportion of Deaths</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1841-01-01</td>\n",
       "      <td>254</td>\n",
       "      <td>37</td>\n",
       "      <td>0.145669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1841-02-01</td>\n",
       "      <td>239</td>\n",
       "      <td>18</td>\n",
       "      <td>0.075314</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1841-03-01</td>\n",
       "      <td>277</td>\n",
       "      <td>12</td>\n",
       "      <td>0.043321</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1841-04-01</td>\n",
       "      <td>255</td>\n",
       "      <td>4</td>\n",
       "      <td>0.015686</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1841-05-01</td>\n",
       "      <td>255</td>\n",
       "      <td>2</td>\n",
       "      <td>0.007843</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date  births  deaths  Proportion of Deaths\n",
       "0  1841-01-01     254      37              0.145669\n",
       "1  1841-02-01     239      18              0.075314\n",
       "2  1841-03-01     277      12              0.043321\n",
       "3  1841-04-01     255       4              0.015686\n",
       "4  1841-05-01     255       2              0.007843"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Proportion of deaths per month\n",
    "monthly_df[\"Proportion of Deaths\"]= monthly_df[\"deaths\"] / monthly_df[\"births\"]\n",
    "monthly_df.head(5)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a30893c",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-7751760b96f6fffc",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "Dr Semmelweis ordered the doctors to wash their hands and made it obligatory in the summer of 1847 to see if that will affect the number of deaths, and since we have the monthly data now, we can trace the number of deaths before and after the handwashing started. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5a783fec",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-192d01798b353bbc",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "monthly_df.dtypes\n",
    "monthly_df['date'] =  pd.to_datetime(monthly_df['date'])\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c06ef0b9",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-32181f02cd7290c3",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "start_handwashing = pd.to_datetime('1847-06-01')\n",
    "\n",
    "before_washing = monthly_df[monthly_df[\"date\"] < start_handwashing]\n",
    "after_washing = monthly_df[monthly_df[\"date\"] >= start_handwashing]\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "38a69a94",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-4902c35be4564564",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Proportion of Deaths')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 59192 characters]\n",
      "text/plain": [
       "<Figure size 1000x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "fig,ax = plt.subplots(figsize = (10,4))\n",
    "x= before_washing[\"date\"]\n",
    "y= before_washing[\"Proportion of Deaths\"]\n",
    "plt.plot(x, y, color= \"orange\")\n",
    "plt.title(\"Before Handwashing\", fontsize=16)\n",
    "plt.xlabel(\"Date\", fontsize=14)\n",
    "plt.ylabel(\"Proportion of Deaths\", fontsize=14)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2160a3a4",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-fc9c02b21b48514c",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Proportion of Deaths')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 54148 characters]\n",
      "text/plain": [
       "<Figure size 1000x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "fig,ax = plt.subplots(figsize = (10,4))\n",
    "x= after_washing[\"date\"]\n",
    "y= after_washing[\"Proportion of Deaths\"]\n",
    "plt.plot(x, y, color= \"green\")\n",
    "plt.title(\"After Handwashing\", fontsize=16)\n",
    "plt.xlabel(\"Date\", fontsize=14)\n",
    "plt.ylabel(\"Proportion of Deaths\", fontsize=14)\n",
    "### END SOLUTION "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecc54d81",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-381b2a7a41cb277f",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    }
   },
   "source": [
    "To see the difference clearly, let's combine the 2 plots in one chart."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "49c85211",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-5cde52a4d4c5d05a",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot: xlabel='date', ylabel='Proportion deaths'>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 53904 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "ax= before_washing.plot(x= \"date\", y= \"Proportion of Deaths\", label= \"Before Handwashing\", color=\"orange\")\n",
    "after_washing.plot(x= \"date\", y= \"Proportion of Deaths\", label= \"After Handwashing\", ax=ax, ylabel= \"Proportion deaths\", color=\"green\") \n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "297582c8",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-ca87810a372a6b9d",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.10504998260908789\n",
      "0.021093375097254535\n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "before_proportion = before_washing[\"Proportion of Deaths\"]\n",
    "after_proportion = after_washing[\"Proportion of Deaths\"]\n",
    "print(before_proportion.mean())\n",
    "print(after_proportion.mean())\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9f12f4bb",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-04582877f1070b3f",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.08395660751183336"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Difference between both proportions\n",
    "mean_diff = after_proportion.mean() - before_proportion.mean()\n",
    "mean_diff\n",
    "# The minus sign indicate that there is a decrease. So handwashing decreased the proportion of deaths from 10% to 2% i.e, by approximately 8%.\n",
    "### END SOLUTION"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
