Note: The default ITS GitLab runner is a shared resource and is subject to slowdowns during heavy usage.
You can run your own GitLab runner that is dedicated just to your group if you need to avoid processing delays.

Commit 9dfd7f34 authored by Liwen Huang's avatar Liwen Huang
Browse files

Upload New File

parent ca0977cb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question 2¶\n",
"It would be interesting to see if there is any evidence of a link between vaccine effectiveness and sex of the child. Calculate the ratio of the number of children who contracted chickenpox but were vaccinated against it (at least one varicella dose) versus those who were vaccinated but did not contract chicken pox. Return results by sex.\n",
"\n",
"This function should return a dictionary in the form of (use the correct numbers):\n",
"\n",
" {\"male\":0.2,\n",
" \"female\":0.4}\n",
"Note: To aid in verification, the chickenpox_by_sex()['female'] value the autograder is looking for starts with the digits 0.00779."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"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>SEQNUMC</th>\n",
" <th>SEQNUMHH</th>\n",
" <th>PDAT</th>\n",
" <th>PROVWT_D</th>\n",
" <th>RDDWT_D</th>\n",
" <th>STRATUM</th>\n",
" <th>YEAR</th>\n",
" <th>AGECPOXR</th>\n",
" <th>HAD_CPOX</th>\n",
" <th>AGEGRP</th>\n",
" <th>...</th>\n",
" <th>XVRCTY2</th>\n",
" <th>XVRCTY3</th>\n",
" <th>XVRCTY4</th>\n",
" <th>XVRCTY5</th>\n",
" <th>XVRCTY6</th>\n",
" <th>XVRCTY7</th>\n",
" <th>XVRCTY8</th>\n",
" <th>XVRCTY9</th>\n",
" <th>INS_STAT2_I</th>\n",
" <th>INS_BREAK_I</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>128521</td>\n",
" <td>12852</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>235.916956</td>\n",
" <td>1031</td>\n",
" <td>2017</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>10741</td>\n",
" <td>1074</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>957.353840</td>\n",
" <td>1068</td>\n",
" <td>2017</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>220011</td>\n",
" <td>22001</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" <td>189.611299</td>\n",
" <td>1050</td>\n",
" <td>2017</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>3</td>\n",
" <td>...</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>86131</td>\n",
" <td>8613</td>\n",
" <td>1</td>\n",
" <td>675.430817</td>\n",
" <td>333.447418</td>\n",
" <td>1040</td>\n",
" <td>2017</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>227141</td>\n",
" <td>22714</td>\n",
" <td>1</td>\n",
" <td>482.617748</td>\n",
" <td>278.768063</td>\n",
" <td>1008</td>\n",
" <td>2017</td>\n",
" <td>NaN</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td></td>\n",
" <td></td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>2.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 453 columns</p>\n",
"</div>"
],
"text/plain": [
" SEQNUMC SEQNUMHH PDAT PROVWT_D RDDWT_D STRATUM YEAR AGECPOXR \\\n",
"1 128521 12852 2 NaN 235.916956 1031 2017 NaN \n",
"2 10741 1074 2 NaN 957.353840 1068 2017 NaN \n",
"3 220011 22001 2 NaN 189.611299 1050 2017 NaN \n",
"4 86131 8613 1 675.430817 333.447418 1040 2017 NaN \n",
"5 227141 22714 1 482.617748 278.768063 1008 2017 NaN \n",
"\n",
" HAD_CPOX AGEGRP ... XVRCTY2 XVRCTY3 XVRCTY4 XVRCTY5 XVRCTY6 \\\n",
"1 2 1 ... NaN NaN NaN \n",
"2 2 1 ... NaN NaN NaN \n",
"3 2 3 ... NaN NaN NaN \n",
"4 2 1 ... NaN NaN NaN \n",
"5 2 1 ... NaN NaN NaN \n",
"\n",
" XVRCTY7 XVRCTY8 XVRCTY9 INS_STAT2_I INS_BREAK_I \n",
"1 NaN NaN NaN NaN NaN \n",
"2 NaN NaN NaN NaN NaN \n",
"3 NaN NaN NaN NaN NaN \n",
"4 NaN NaN NaN 1.0 2.0 \n",
"5 NaN NaN NaN 2.0 1.0 \n",
"\n",
"[5 rows x 453 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"df = pd.read_csv('datasets/NISPUF17.csv', index_col=0)\n",
"\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"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>SEX</th>\n",
" <th>HAD_CPOX</th>\n",
" <th>P_NUMVRC</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" SEX HAD_CPOX P_NUMVRC\n",
"1 1 2 NaN\n",
"2 1 2 NaN\n",
"3 2 2 NaN\n",
"4 2 2 1.0\n",
"5 2 2 0.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chc = df[['SEX','HAD_CPOX','P_NUMVRC']]\n",
"chc.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 False\n",
"2 False\n",
"3 False\n",
"4 False\n",
"5 False\n",
"Name: HAD_CPOX, dtype: bool"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chc_had = df['HAD_CPOX'].gt(3)\n",
"chc_had.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<bound method NDFrame.head of 1 False\n",
"2 False\n",
"3 False\n",
"4 True\n",
"5 False\n",
" ... \n",
"28461 False\n",
"28462 False\n",
"28463 False\n",
"28464 False\n",
"28465 False\n",
"Name: P_NUMVRC, Length: 28465, dtype: bool>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chc_num = df['P_NUMVRC'].gt(0)\n",
"chc_num.head"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1 False\n",
"2 False\n",
"3 False\n",
"4 False\n",
"5 False\n",
"dtype: bool"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chc_sex = df['HAD_CPOX'].gt(3) & df['P_NUMVRC'].gt(0)\n",
"chc_sex.head()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"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>HAD_CPOX</th>\n",
" <th>SEX</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>2</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" HAD_CPOX SEX\n",
"4 2 2\n",
"7 2 2\n",
"11 2 1\n",
"13 2 1\n",
"17 2 1"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chc_sex = df[df['HAD_CPOX'].lt(3) & df['P_NUMVRC'].gt(0)].loc[:,['HAD_CPOX','SEX']]\n",
"chc_sex.head()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# HAD_CPOX: 1-Y; 2-N\n",
"# SEX: 1-M; 2-F"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"68"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chc_1 = len(chc_sex[(chc_sex['HAD_CPOX'] == 1) & (chc_sex['SEX'] == 1)])\n",
"chc_1"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"53"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chc_2 = len(chc_sex[(chc_sex['HAD_CPOX'] == 1) & (chc_sex['SEX'] == 2)])\n",
"chc_2"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7028"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chn_1 = len(chc_sex[(chc_sex['HAD_CPOX'] == 2) & (chc_sex['SEX'] == 1)])\n",
"chn_1"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6802"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chn_2 = len(chc_sex[(chc_sex['HAD_CPOX'] == 2) & (chc_sex['SEX'] == 2)])\n",
"chn_2"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'male': 0.009675583380762664, 'female': 0.0077918259335489565}"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ratio_chc = {'male':0,\n",
" 'female':0}\n",
"\n",
"ratio_chc['male'] = chc_1/chn_1\n",
"ratio_chc['female'] = chc_2/chn_2\n",
"\n",
"ratio_chc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment