adds exercises for tabular data part
This commit is contained in:
parent
2e60b94c52
commit
26eb146a5c
16 changed files with 60195 additions and 0 deletions
6288
data/predimed_mapping.csv
Normal file
6288
data/predimed_mapping.csv
Normal file
File diff suppressed because it is too large
Load diff
6325
data/predimed_records.csv
Normal file
6325
data/predimed_records.csv
Normal file
File diff suppressed because it is too large
Load diff
43
exercises/tabular_join/dropped.csv
Normal file
43
exercises/tabular_join/dropped.csv
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
location-id,patient-id
|
||||||
|
1,217
|
||||||
|
1,1147
|
||||||
|
1,1170
|
||||||
|
1,627
|
||||||
|
4,541
|
||||||
|
4,72
|
||||||
|
4,727
|
||||||
|
2,500
|
||||||
|
3,177
|
||||||
|
5,1123
|
||||||
|
1,1109
|
||||||
|
4,1027
|
||||||
|
3,658
|
||||||
|
1,1241
|
||||||
|
3,1004
|
||||||
|
3,664
|
||||||
|
4,1248
|
||||||
|
1,937
|
||||||
|
5,606
|
||||||
|
4,926
|
||||||
|
5,327
|
||||||
|
1,1244
|
||||||
|
4,1001
|
||||||
|
1,959
|
||||||
|
5,246
|
||||||
|
5,55
|
||||||
|
1,899
|
||||||
|
1,991
|
||||||
|
4,915
|
||||||
|
3,4
|
||||||
|
5,465
|
||||||
|
5,235
|
||||||
|
2,1022
|
||||||
|
2,889
|
||||||
|
4,789
|
||||||
|
5,1154
|
||||||
|
5,35
|
||||||
|
3,990
|
||||||
|
2,98
|
||||||
|
1,580
|
||||||
|
1,807
|
||||||
|
3,584
|
|
657
exercises/tabular_join/tabular_join.ipynb
Normal file
657
exercises/tabular_join/tabular_join.ipynb
Normal file
|
@ -0,0 +1,657 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "f11a76bf",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Exercise on Joins and anti-joins: add information from other tables"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "b6f2742b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "2967c84e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load data from clinical trial\n",
|
||||||
|
"\n",
|
||||||
|
"Data comes in two different files. The file `predimed_records.csv` file contains the clinical data for each patient, except which diet group they were assigned. The file `predimed_mapping.csv` contain the information of which patient was assigned to which diet group. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "ed626ee3",
|
||||||
|
"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>patient-id</th>\n",
|
||||||
|
" <th>location-id</th>\n",
|
||||||
|
" <th>sex</th>\n",
|
||||||
|
" <th>age</th>\n",
|
||||||
|
" <th>smoke</th>\n",
|
||||||
|
" <th>bmi</th>\n",
|
||||||
|
" <th>waist</th>\n",
|
||||||
|
" <th>wth</th>\n",
|
||||||
|
" <th>htn</th>\n",
|
||||||
|
" <th>diab</th>\n",
|
||||||
|
" <th>hyperchol</th>\n",
|
||||||
|
" <th>famhist</th>\n",
|
||||||
|
" <th>hormo</th>\n",
|
||||||
|
" <th>p14</th>\n",
|
||||||
|
" <th>toevent</th>\n",
|
||||||
|
" <th>event</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>436</td>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>Male</td>\n",
|
||||||
|
" <td>58</td>\n",
|
||||||
|
" <td>Former</td>\n",
|
||||||
|
" <td>33.53</td>\n",
|
||||||
|
" <td>122</td>\n",
|
||||||
|
" <td>0.753086</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>10</td>\n",
|
||||||
|
" <td>5.374401</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>1130</td>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>Male</td>\n",
|
||||||
|
" <td>77</td>\n",
|
||||||
|
" <td>Current</td>\n",
|
||||||
|
" <td>31.05</td>\n",
|
||||||
|
" <td>119</td>\n",
|
||||||
|
" <td>0.730061</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>10</td>\n",
|
||||||
|
" <td>6.097194</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>1131</td>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>72</td>\n",
|
||||||
|
" <td>Former</td>\n",
|
||||||
|
" <td>30.86</td>\n",
|
||||||
|
" <td>106</td>\n",
|
||||||
|
" <td>0.654321</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>8</td>\n",
|
||||||
|
" <td>5.946612</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>1132</td>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>Male</td>\n",
|
||||||
|
" <td>71</td>\n",
|
||||||
|
" <td>Former</td>\n",
|
||||||
|
" <td>27.68</td>\n",
|
||||||
|
" <td>118</td>\n",
|
||||||
|
" <td>0.694118</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>8</td>\n",
|
||||||
|
" <td>2.907598</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>1111</td>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>79</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>35.94</td>\n",
|
||||||
|
" <td>129</td>\n",
|
||||||
|
" <td>0.806250</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>4.761123</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" patient-id location-id sex age smoke bmi waist wth htn \\\n",
|
||||||
|
"0 436 4 Male 58 Former 33.53 122 0.753086 No \n",
|
||||||
|
"1 1130 4 Male 77 Current 31.05 119 0.730061 Yes \n",
|
||||||
|
"2 1131 4 Female 72 Former 30.86 106 0.654321 No \n",
|
||||||
|
"3 1132 4 Male 71 Former 27.68 118 0.694118 Yes \n",
|
||||||
|
"4 1111 2 Female 79 Never 35.94 129 0.806250 Yes \n",
|
||||||
|
"\n",
|
||||||
|
" diab hyperchol famhist hormo p14 toevent event \n",
|
||||||
|
"0 No Yes No No 10 5.374401 Yes \n",
|
||||||
|
"1 Yes No No No 10 6.097194 No \n",
|
||||||
|
"2 Yes No Yes No 8 5.946612 No \n",
|
||||||
|
"3 No Yes No No 8 2.907598 Yes \n",
|
||||||
|
"4 No Yes No No 9 4.761123 No "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df = pd.read_csv('../../data/predimed_records.csv')\n",
|
||||||
|
"df.head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "48d5375f",
|
||||||
|
"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>location-id</th>\n",
|
||||||
|
" <th>patient-id</th>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>885</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>182</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>971</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>691</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>632</td>\n",
|
||||||
|
" <td>Control</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" location-id patient-id group\n",
|
||||||
|
"0 2 885 MedDiet + VOO\n",
|
||||||
|
"1 1 182 MedDiet + Nuts\n",
|
||||||
|
"2 1 971 MedDiet + Nuts\n",
|
||||||
|
"3 2 691 MedDiet + Nuts\n",
|
||||||
|
"4 2 632 Control"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"info = pd.read_csv('../../data/predimed_mapping.csv')\n",
|
||||||
|
"info.head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "2b4b98ed-d7ec-4b7c-b983-adc616d2f16f",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"There were 5 different locations where the study was conducted, each one gave an identification number `patient-id` to each participant."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "b9dbc492-1489-4530-96ac-5f33f7389caa",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"array([2, 1, 3, 4, 5])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"info['location-id'].unique()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "2fef4d37",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 1. Add diet information to the patients' records\n",
|
||||||
|
"\n",
|
||||||
|
"* For how many patients do we have clinical information? (i.e., rows in `df`)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "861ac334-14ce-490a-b3c4-877b32789f3e",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## your code here\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1c1701e2-c295-4032-9e89-0d8470f41593",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* For how many patients do we have diet information? (i.e., rows in `info`)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "14f57842-5722-4953-88d6-d7cf3070400c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## your code here\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "3f23fa17-af3e-41c3-883f-3e1279d4820e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Perform the merge, keeping in mind that it only make sense to analyze patients with the diet information. \n",
|
||||||
|
"* Which type of merge would you do? \n",
|
||||||
|
"* For how many patients do we have full information (records and which diet they followed? "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "35e19a53",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## your code here\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "946beb08-30a5-4020-8612-360385cdfc1e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 2. Add location information to the patients' records\n",
|
||||||
|
"\n",
|
||||||
|
"There were five locations where the study was conducted. Here is a DataFrame containing the information of each location. \n",
|
||||||
|
"\n",
|
||||||
|
"- Add a new column to the dataset that contains the city where each patient was recorded.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "36ce0688-d421-4a07-b00e-0e9b3201f0e0",
|
||||||
|
"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>location-id</th>\n",
|
||||||
|
" <th>City</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>Valencia</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>3</td>\n",
|
||||||
|
" <td>Barcelona</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>Bilbao</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>Malaga</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" location-id City\n",
|
||||||
|
"0 1 Madrid\n",
|
||||||
|
"1 2 Valencia\n",
|
||||||
|
"2 3 Barcelona\n",
|
||||||
|
"3 4 Bilbao\n",
|
||||||
|
"4 5 Malaga"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"locations = pd.DataFrame.from_dict({'location-id': [1, 2, 3, 4, 5], \n",
|
||||||
|
" 'City': ['Madrid', 'Valencia', 'Barcelona', 'Bilbao','Malaga']})\n",
|
||||||
|
"locations"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "b636dde4-129a-4dd1-8cbf-c539c9c8a5f2",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"## your code here:\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "44031178",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 3. Remove drops from table\n",
|
||||||
|
"\n",
|
||||||
|
"Some patients drop from the study early on and they should be removed from our analysis. Their IDS are stored in file `dropped.csv`.\n",
|
||||||
|
"1. Load the list of patients who droped, from `dropped.csv`\n",
|
||||||
|
"2. Use an anti-join to remove them from the table\n",
|
||||||
|
"3. How many patients (rows) are left in the data?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"id": "d1d4cc27",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dropped = pd.read_csv('dropped.csv')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"id": "fbebbd97",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(42, 2)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"dropped.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 12,
|
||||||
|
"id": "8a3c7943",
|
||||||
|
"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>location-id</th>\n",
|
||||||
|
" <th>patient-id</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>217</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>1147</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>1170</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>627</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>541</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" location-id patient-id\n",
|
||||||
|
"0 1 217\n",
|
||||||
|
"1 1 1147\n",
|
||||||
|
"2 1 1170\n",
|
||||||
|
"3 1 627\n",
|
||||||
|
"4 4 541"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"dropped.head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"id": "573687e7",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# your code here\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "84270332",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 4. Save final result in `processed_data_predimed.csv`\n",
|
||||||
|
"\n",
|
||||||
|
"1. Using the `.to_csv` method of Pandas DataFrames"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"id": "85902eea",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fname = 'processed_data_predimed.csv'\n",
|
||||||
|
"\n",
|
||||||
|
"# your code here\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.11.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
1908
exercises/tabular_join/tabular_join_solution.ipynb
Normal file
1908
exercises/tabular_join/tabular_join_solution.ipynb
Normal file
File diff suppressed because it is too large
Load diff
6246
exercises/tabular_split_apply_combine/processed_data_predimed.csv
Normal file
6246
exercises/tabular_split_apply_combine/processed_data_predimed.csv
Normal file
File diff suppressed because it is too large
Load diff
420
exercises/tabular_split_apply_combine/split_apply_combine.ipynb
Normal file
420
exercises/tabular_split_apply_combine/split_apply_combine.ipynb
Normal file
|
@ -0,0 +1,420 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "6f6aa857",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Exercise: Compute summary statistics"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "8f9bc8b1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"import pandas as pd"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1be11d54",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load the patient data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "d2dfebd3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df = pd.read_csv('processed_data_predimed.csv')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "09554c84",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(6245, 18)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "df95a10b",
|
||||||
|
"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>patient-id</th>\n",
|
||||||
|
" <th>location-id</th>\n",
|
||||||
|
" <th>sex</th>\n",
|
||||||
|
" <th>age</th>\n",
|
||||||
|
" <th>smoke</th>\n",
|
||||||
|
" <th>bmi</th>\n",
|
||||||
|
" <th>waist</th>\n",
|
||||||
|
" <th>wth</th>\n",
|
||||||
|
" <th>htn</th>\n",
|
||||||
|
" <th>diab</th>\n",
|
||||||
|
" <th>hyperchol</th>\n",
|
||||||
|
" <th>famhist</th>\n",
|
||||||
|
" <th>hormo</th>\n",
|
||||||
|
" <th>p14</th>\n",
|
||||||
|
" <th>toevent</th>\n",
|
||||||
|
" <th>event</th>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th>City</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>77</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>25.92</td>\n",
|
||||||
|
" <td>94</td>\n",
|
||||||
|
" <td>0.657343</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>5.538672</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>68</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>34.85</td>\n",
|
||||||
|
" <td>150</td>\n",
|
||||||
|
" <td>0.949367</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>10</td>\n",
|
||||||
|
" <td>3.063655</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>3</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>66</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>37.50</td>\n",
|
||||||
|
" <td>120</td>\n",
|
||||||
|
" <td>0.750000</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>6</td>\n",
|
||||||
|
" <td>5.590691</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>77</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>29.26</td>\n",
|
||||||
|
" <td>93</td>\n",
|
||||||
|
" <td>0.628378</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>6</td>\n",
|
||||||
|
" <td>5.456537</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>60</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>30.02</td>\n",
|
||||||
|
" <td>104</td>\n",
|
||||||
|
" <td>0.662420</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>2.746064</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Control</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" patient-id location-id sex age smoke bmi waist wth htn \\\n",
|
||||||
|
"0 1 1 Female 77 Never 25.92 94 0.657343 Yes \n",
|
||||||
|
"1 2 1 Female 68 Never 34.85 150 0.949367 Yes \n",
|
||||||
|
"2 3 1 Female 66 Never 37.50 120 0.750000 Yes \n",
|
||||||
|
"3 4 1 Female 77 Never 29.26 93 0.628378 Yes \n",
|
||||||
|
"4 5 1 Female 60 Never 30.02 104 0.662420 Yes \n",
|
||||||
|
"\n",
|
||||||
|
" diab hyperchol famhist hormo p14 toevent event group City \n",
|
||||||
|
"0 No Yes Yes No 9 5.538672 No MedDiet + VOO Madrid \n",
|
||||||
|
"1 No Yes Yes NaN 10 3.063655 No MedDiet + Nuts Madrid \n",
|
||||||
|
"2 Yes No No No 6 5.590691 No MedDiet + Nuts Madrid \n",
|
||||||
|
"3 Yes No No No 6 5.456537 No MedDiet + VOO Madrid \n",
|
||||||
|
"4 No Yes No No 9 2.746064 No Control Madrid "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df.head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "0b4f6091",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 1. Did the mediterranean diet help prevent cardiovascular events?\n",
|
||||||
|
"\n",
|
||||||
|
"To answer this question, we need to compute how many cardiovascular \"events\" occured in each group of participants, separated by the diet they followed.\n",
|
||||||
|
"In the data the column `event` contains `Yes` or `No`, indicating if that patient had an cardiovascular event. The column `group` contains which diet they followed.\n",
|
||||||
|
"\n",
|
||||||
|
"We first convert the column ``event'' to a binary value."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "99b21627-1b48-44ee-bda2-312b0718bd59",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df['event'] = df['event'].map({'Yes': 1, 'No': 0})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "a63e59c5-fe50-433f-a529-f601c795db67",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* Now compute the total number of events by diet group. Compare the numbers and see if you can answer the question."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "00bb9eb1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# your code here:\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "b052a40a-ae68-4376-8557-541eafb3face",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* Check how many patients had each group"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "db946d0f-8204-43a3-853c-41981a9811f4",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# your code here:\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "64339449-d766-4a2a-85d3-8aafac5533b7",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"There were no equal number in each group, so to be precise we need to put the numbers into perspective of the total. For that:\n",
|
||||||
|
"* Calculate how many events occured relative to the amount of patients in each group (in percentage). \n",
|
||||||
|
"Do this sepearated by diet group."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "13ad4130-2094-4e7a-a416-f0fd6e810413",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# your code here:\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9655c0ac-18e9-4297-9f6e-557bfe95ed5e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"It seems that the control group had a higher percentage of events than the other two"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1940d3fe",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 2. Smoking\n",
|
||||||
|
"\n",
|
||||||
|
"Did smoking make a difference in the outcome of the study?\n",
|
||||||
|
"Calculate how many events occured by diet group *and* smoking. The idea is that you arrive to a table like this:\n",
|
||||||
|
"\n",
|
||||||
|
"| group | Current | Former | Never |\n",
|
||||||
|
"|:---------------|----------:|---------:|--------:|\n",
|
||||||
|
"| Control | ... | ... | ... |\n",
|
||||||
|
"| MedDiet + Nuts | ... | ... | ... |\n",
|
||||||
|
"| MedDiet + VOO | ... | ... | ... |\n",
|
||||||
|
"\n",
|
||||||
|
"where each entry in the table has the percentage of events for each group. \n",
|
||||||
|
"\n",
|
||||||
|
"Hint: use `pivot_table`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "5ab4e70e-6261-4a26-8ad9-14eae15be09c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# your code here\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "511c640e-8f0f-449f-af33-85061d89cfd3",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 3. Age differences?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1a04ea5c-8a27-4e67-aafa-ba34580a8d7f",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Finally, check that there were no big differences in the age between the groups.\n",
|
||||||
|
"* Calculate the mean and standard deviation of the patient's age, separated by diet group.\n",
|
||||||
|
"\n",
|
||||||
|
"You should be getting a table where diet group are in the rows and gender in columns, like this\n",
|
||||||
|
"\n",
|
||||||
|
"| group | Female | Male |\n",
|
||||||
|
"|:---------------|---------:|-------:|\n",
|
||||||
|
"| Control | 68 | 66.4 |\n",
|
||||||
|
"| MedDiet + Nuts | 67.4 | 65.8 |\n",
|
||||||
|
"| MedDiet + VOO | 67.7 | 66.1 |\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"id": "196fd111-72bc-4b87-b8fb-293547a8c83d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# your code here:\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.11.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
|
@ -0,0 +1,927 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "6f6aa857",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Exercise: Compute summary statistics"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "8f9bc8b1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%matplotlib inline\n",
|
||||||
|
"\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"import pandas as pd\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1be11d54",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load the patient data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 25,
|
||||||
|
"id": "d2dfebd3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df = pd.read_csv('processed_data_predimed.csv')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 26,
|
||||||
|
"id": "09554c84",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(6245, 17)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 26,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 27,
|
||||||
|
"id": "df95a10b",
|
||||||
|
"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>patient-id</th>\n",
|
||||||
|
" <th>location-id</th>\n",
|
||||||
|
" <th>sex</th>\n",
|
||||||
|
" <th>age</th>\n",
|
||||||
|
" <th>smoke</th>\n",
|
||||||
|
" <th>bmi</th>\n",
|
||||||
|
" <th>waist</th>\n",
|
||||||
|
" <th>wth</th>\n",
|
||||||
|
" <th>htn</th>\n",
|
||||||
|
" <th>diab</th>\n",
|
||||||
|
" <th>hyperchol</th>\n",
|
||||||
|
" <th>famhist</th>\n",
|
||||||
|
" <th>hormo</th>\n",
|
||||||
|
" <th>p14</th>\n",
|
||||||
|
" <th>toevent</th>\n",
|
||||||
|
" <th>event</th>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>77</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>25.92</td>\n",
|
||||||
|
" <td>94</td>\n",
|
||||||
|
" <td>0.657343</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>5.538672</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>68</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>34.85</td>\n",
|
||||||
|
" <td>150</td>\n",
|
||||||
|
" <td>0.949367</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>10</td>\n",
|
||||||
|
" <td>3.063655</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>3</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>66</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>37.50</td>\n",
|
||||||
|
" <td>120</td>\n",
|
||||||
|
" <td>0.750000</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>6</td>\n",
|
||||||
|
" <td>5.590691</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>77</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>29.26</td>\n",
|
||||||
|
" <td>93</td>\n",
|
||||||
|
" <td>0.628378</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>6</td>\n",
|
||||||
|
" <td>5.456537</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>60</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>30.02</td>\n",
|
||||||
|
" <td>104</td>\n",
|
||||||
|
" <td>0.662420</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>2.746064</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Control</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" patient-id location-id sex age smoke bmi waist wth htn \\\n",
|
||||||
|
"0 1 1 Female 77 Never 25.92 94 0.657343 Yes \n",
|
||||||
|
"1 2 1 Female 68 Never 34.85 150 0.949367 Yes \n",
|
||||||
|
"2 3 1 Female 66 Never 37.50 120 0.750000 Yes \n",
|
||||||
|
"3 4 1 Female 77 Never 29.26 93 0.628378 Yes \n",
|
||||||
|
"4 5 1 Female 60 Never 30.02 104 0.662420 Yes \n",
|
||||||
|
"\n",
|
||||||
|
" diab hyperchol famhist hormo p14 toevent event group \n",
|
||||||
|
"0 No Yes Yes No 9 5.538672 No MedDiet + VOO \n",
|
||||||
|
"1 No Yes Yes NaN 10 3.063655 No MedDiet + Nuts \n",
|
||||||
|
"2 Yes No No No 6 5.590691 No MedDiet + Nuts \n",
|
||||||
|
"3 Yes No No No 6 5.456537 No MedDiet + VOO \n",
|
||||||
|
"4 No Yes No No 9 2.746064 No Control "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 27,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df.head()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "0b4f6091",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 1. Did the mediterranean diet help prevent cardiovascular events?\n",
|
||||||
|
"\n",
|
||||||
|
"To answer this question, we need to compute how many cardiovascular \"events\" occured in each group of participants, separated by the diet they followed.\n",
|
||||||
|
"In the data the column `event` contains `Yes` or `No`, indicating if that patient had an cardiovascular event. The column `group` contains which diet they followed.\n",
|
||||||
|
"\n",
|
||||||
|
"* Convert the column `event` from string to binary (1 for Yes, 0 for No) (this will ease the calculations that follow later).\n",
|
||||||
|
" Hint: use the method `.map()`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 28,
|
||||||
|
"id": "99b21627-1b48-44ee-bda2-312b0718bd59",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"df['event'] = df['event'].map({'Yes': 1, 'No': 0})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "a63e59c5-fe50-433f-a529-f601c795db67",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* Now compute the total number of events by diet group. Compare the numbers and see if you can answer the question."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 29,
|
||||||
|
"id": "00bb9eb1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"group\n",
|
||||||
|
"Control 96\n",
|
||||||
|
"MedDiet + Nuts 69\n",
|
||||||
|
"MedDiet + VOO 83\n",
|
||||||
|
"Name: event, dtype: int64"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 29,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df.groupby('group')['event'].sum()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "b052a40a-ae68-4376-8557-541eafb3face",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"* Check how many patients had each group"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 30,
|
||||||
|
"id": "db946d0f-8204-43a3-853c-41981a9811f4",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"group\n",
|
||||||
|
"Control 2016\n",
|
||||||
|
"MedDiet + Nuts 2077\n",
|
||||||
|
"MedDiet + VOO 2152\n",
|
||||||
|
"Name: event, dtype: int64"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 30,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df.groupby('group')['event'].count()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "64339449-d766-4a2a-85d3-8aafac5533b7",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"There were no equal number in each group, so to be precise we need to put the numbers into perspective of the total. For that:\n",
|
||||||
|
"* Calculate how many events occured relative to the amount of patients in each group (in percentage). \n",
|
||||||
|
"Do this sepearated by diet group."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 31,
|
||||||
|
"id": "13ad4130-2094-4e7a-a416-f0fd6e810413",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"group\n",
|
||||||
|
"Control 4.761905\n",
|
||||||
|
"MedDiet + Nuts 3.322099\n",
|
||||||
|
"MedDiet + VOO 3.856877\n",
|
||||||
|
"Name: event, dtype: float64"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 31,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df.groupby('group')['event'].sum()*100 / df.groupby('group')['event'].count()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9655c0ac-18e9-4297-9f6e-557bfe95ed5e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"It seems that the control group had a higher percentage of events than the other two"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1940d3fe",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 2. Smoking\n",
|
||||||
|
"\n",
|
||||||
|
"Did smoking make a difference in the outcome of the study?\n",
|
||||||
|
"Calculate how many events occured by diet group *and* smoking. The idea is that you arrive to a table like this:\n",
|
||||||
|
"\n",
|
||||||
|
"| group | Current | Former | Never |\n",
|
||||||
|
"|:---------------|----------:|---------:|--------:|\n",
|
||||||
|
"| Control | ... | ... | ... |\n",
|
||||||
|
"| MedDiet + Nuts | ... | ... | ... |\n",
|
||||||
|
"| MedDiet + VOO | ... | ... | ... |\n",
|
||||||
|
"\n",
|
||||||
|
"where each entry in the table has the percentage of events for each group\n",
|
||||||
|
"Hint: use `pivot_table`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 32,
|
||||||
|
"id": "5ab4e70e-6261-4a26-8ad9-14eae15be09c",
|
||||||
|
"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>smoke</th>\n",
|
||||||
|
" <th>Current</th>\n",
|
||||||
|
" <th>Former</th>\n",
|
||||||
|
" <th>Never</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>Control</th>\n",
|
||||||
|
" <td>13</td>\n",
|
||||||
|
" <td>39</td>\n",
|
||||||
|
" <td>44</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + Nuts</th>\n",
|
||||||
|
" <td>15</td>\n",
|
||||||
|
" <td>20</td>\n",
|
||||||
|
" <td>34</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + VOO</th>\n",
|
||||||
|
" <td>20</td>\n",
|
||||||
|
" <td>29</td>\n",
|
||||||
|
" <td>34</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"smoke Current Former Never\n",
|
||||||
|
"group \n",
|
||||||
|
"Control 13 39 44\n",
|
||||||
|
"MedDiet + Nuts 15 20 34\n",
|
||||||
|
"MedDiet + VOO 20 29 34"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 32,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"counts = df.pivot_table(index='group', columns='smoke', values='event', aggfunc='sum')\n",
|
||||||
|
"counts"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 17,
|
||||||
|
"id": "f854f24d-9108-42bc-a23b-6b5503f5deba",
|
||||||
|
"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>smoke</th>\n",
|
||||||
|
" <th>Current</th>\n",
|
||||||
|
" <th>Former</th>\n",
|
||||||
|
" <th>Never</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>Control</th>\n",
|
||||||
|
" <td>264</td>\n",
|
||||||
|
" <td>485</td>\n",
|
||||||
|
" <td>1267</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + Nuts</th>\n",
|
||||||
|
" <td>291</td>\n",
|
||||||
|
" <td>539</td>\n",
|
||||||
|
" <td>1247</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + VOO</th>\n",
|
||||||
|
" <td>290</td>\n",
|
||||||
|
" <td>531</td>\n",
|
||||||
|
" <td>1331</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"smoke Current Former Never\n",
|
||||||
|
"group \n",
|
||||||
|
"Control 264 485 1267\n",
|
||||||
|
"MedDiet + Nuts 291 539 1247\n",
|
||||||
|
"MedDiet + VOO 290 531 1331"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 17,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"N = df.pivot_table(index='group', columns='smoke', values='event', aggfunc='count')\n",
|
||||||
|
"N"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
|
"id": "1358f6b8-60a5-44db-ba23-4ef7c7af8455",
|
||||||
|
"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>smoke</th>\n",
|
||||||
|
" <th>Current</th>\n",
|
||||||
|
" <th>Former</th>\n",
|
||||||
|
" <th>Never</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>Control</th>\n",
|
||||||
|
" <td>4.924242</td>\n",
|
||||||
|
" <td>8.041237</td>\n",
|
||||||
|
" <td>3.472770</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + Nuts</th>\n",
|
||||||
|
" <td>5.154639</td>\n",
|
||||||
|
" <td>3.710575</td>\n",
|
||||||
|
" <td>2.726544</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + VOO</th>\n",
|
||||||
|
" <td>6.896552</td>\n",
|
||||||
|
" <td>5.461394</td>\n",
|
||||||
|
" <td>2.554470</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"smoke Current Former Never\n",
|
||||||
|
"group \n",
|
||||||
|
"Control 4.924242 8.041237 3.472770\n",
|
||||||
|
"MedDiet + Nuts 5.154639 3.710575 2.726544\n",
|
||||||
|
"MedDiet + VOO 6.896552 5.461394 2.554470"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 18,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"counts*100/N"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "511c640e-8f0f-449f-af33-85061d89cfd3",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 3. Age differences?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1a04ea5c-8a27-4e67-aafa-ba34580a8d7f",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Finally, check that there were no big differences in the age between the groups.\n",
|
||||||
|
"* Calculate the mean and standard deviation of the patient's age, separated by diet group.\n",
|
||||||
|
"\n",
|
||||||
|
"You should be getting a table where diet group are in the rows and gender in columns, like this\n",
|
||||||
|
"\n",
|
||||||
|
"| group | Female | Male |\n",
|
||||||
|
"|:---------------|---------:|-------:|\n",
|
||||||
|
"| Control | 68 | 66.4 |\n",
|
||||||
|
"| MedDiet + Nuts | 67.4 | 65.8 |\n",
|
||||||
|
"| MedDiet + VOO | 67.7 | 66.1 |\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 33,
|
||||||
|
"id": "40d8f79e-5595-4a35-822c-042206bde7db",
|
||||||
|
"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>sex</th>\n",
|
||||||
|
" <th>Female</th>\n",
|
||||||
|
" <th>Male</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>Control</th>\n",
|
||||||
|
" <td>68.0</td>\n",
|
||||||
|
" <td>66.4</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + Nuts</th>\n",
|
||||||
|
" <td>67.4</td>\n",
|
||||||
|
" <td>65.8</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + VOO</th>\n",
|
||||||
|
" <td>67.7</td>\n",
|
||||||
|
" <td>66.1</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"sex Female Male\n",
|
||||||
|
"group \n",
|
||||||
|
"Control 68.0 66.4\n",
|
||||||
|
"MedDiet + Nuts 67.4 65.8\n",
|
||||||
|
"MedDiet + VOO 67.7 66.1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 33,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# this works but it is longer than necessary\n",
|
||||||
|
"df.groupby(['group', 'sex'])['age'].mean().reset_index().pivot_table(index='group', columns='sex', values='age').round(1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 34,
|
||||||
|
"id": "196fd111-72bc-4b87-b8fb-293547a8c83d",
|
||||||
|
"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>sex</th>\n",
|
||||||
|
" <th>Female</th>\n",
|
||||||
|
" <th>Male</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>Control</th>\n",
|
||||||
|
" <td>68.0</td>\n",
|
||||||
|
" <td>66.4</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + Nuts</th>\n",
|
||||||
|
" <td>67.4</td>\n",
|
||||||
|
" <td>65.8</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + VOO</th>\n",
|
||||||
|
" <td>67.7</td>\n",
|
||||||
|
" <td>66.1</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"sex Female Male\n",
|
||||||
|
"group \n",
|
||||||
|
"Control 68.0 66.4\n",
|
||||||
|
"MedDiet + Nuts 67.4 65.8\n",
|
||||||
|
"MedDiet + VOO 67.7 66.1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 34,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# instead of grouping first, do the pivot first and pass the aggregation function as an argument\n",
|
||||||
|
"df.pivot_table(index='group', columns='sex', values='age', aggfunc='mean').round(1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 37,
|
||||||
|
"id": "dd5023cd",
|
||||||
|
"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 tr th {\n",
|
||||||
|
" text-align: left;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe thead tr:last-of-type th {\n",
|
||||||
|
" text-align: right;\n",
|
||||||
|
" }\n",
|
||||||
|
"</style>\n",
|
||||||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||||||
|
" <thead>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th colspan=\"2\" halign=\"left\">mean</th>\n",
|
||||||
|
" <th colspan=\"2\" halign=\"left\">std</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>sex</th>\n",
|
||||||
|
" <th>Female</th>\n",
|
||||||
|
" <th>Male</th>\n",
|
||||||
|
" <th>Female</th>\n",
|
||||||
|
" <th>Male</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>Control</th>\n",
|
||||||
|
" <td>68.0</td>\n",
|
||||||
|
" <td>66.4</td>\n",
|
||||||
|
" <td>6.0</td>\n",
|
||||||
|
" <td>6.6</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + Nuts</th>\n",
|
||||||
|
" <td>67.4</td>\n",
|
||||||
|
" <td>65.8</td>\n",
|
||||||
|
" <td>5.6</td>\n",
|
||||||
|
" <td>6.4</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>MedDiet + VOO</th>\n",
|
||||||
|
" <td>67.7</td>\n",
|
||||||
|
" <td>66.1</td>\n",
|
||||||
|
" <td>5.8</td>\n",
|
||||||
|
" <td>6.6</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" mean std \n",
|
||||||
|
"sex Female Male Female Male\n",
|
||||||
|
"group \n",
|
||||||
|
"Control 68.0 66.4 6.0 6.6\n",
|
||||||
|
"MedDiet + Nuts 67.4 65.8 5.6 6.4\n",
|
||||||
|
"MedDiet + VOO 67.7 66.1 5.8 6.6"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 37,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"# to get the standard deviation you could do the same the last time but pass aggfunc = 'std. This will return you another dataframe.\n",
|
||||||
|
"\n",
|
||||||
|
"# Alternatively, you can calculate both mean and S.D. in one step. For that you can pass more than one value for the aggregation function\n",
|
||||||
|
"df.pivot_table(index='group', columns='sex', values='age', aggfunc=['mean', 'std']).round(1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "0af53833-f872-4bd1-9c67-90a370dfe6c5",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.11.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
787
exercises/tabular_tidy_data/tidy_data.ipynb
Normal file
787
exercises/tabular_tidy_data/tidy_data.ipynb
Normal file
|
@ -0,0 +1,787 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "e951a26e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Exercise: Analysis of tuberculosis cases by country and year period\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "6b181870",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"pd.set_option('display.max_rows', 1000)\n",
|
||||||
|
"pd.set_option('display.max_columns', 100)\n",
|
||||||
|
"pd.set_option(\"display.max_colwidth\", None)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9adcc036",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load the tuberculosis data from the World Health Organization"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "5d9e9162",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"tb_raw = pd.read_csv('who2.csv', index_col='rownames')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "cf7691e5",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Only keep data between 2000 and 2012"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "a953d230",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cols = ['country', 'year'] + [c for c in tb_raw.columns if c.startswith('sp')]\n",
|
||||||
|
"tb_raw = tb_raw.loc[tb_raw['year'].between(2000, 2012), cols]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "ba962fb7",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(2783, 16)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "c79a5b8d",
|
||||||
|
"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>country</th>\n",
|
||||||
|
" <th>year</th>\n",
|
||||||
|
" <th>sp_m_014</th>\n",
|
||||||
|
" <th>sp_m_1524</th>\n",
|
||||||
|
" <th>sp_m_2534</th>\n",
|
||||||
|
" <th>sp_m_3544</th>\n",
|
||||||
|
" <th>sp_m_4554</th>\n",
|
||||||
|
" <th>sp_m_5564</th>\n",
|
||||||
|
" <th>sp_m_65</th>\n",
|
||||||
|
" <th>sp_f_014</th>\n",
|
||||||
|
" <th>sp_f_1524</th>\n",
|
||||||
|
" <th>sp_f_2534</th>\n",
|
||||||
|
" <th>sp_f_3544</th>\n",
|
||||||
|
" <th>sp_f_4554</th>\n",
|
||||||
|
" <th>sp_f_5564</th>\n",
|
||||||
|
" <th>sp_f_65</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>rownames</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>5551</th>\n",
|
||||||
|
" <td>San Marino</td>\n",
|
||||||
|
" <td>2009</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",
|
||||||
|
" <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>642</th>\n",
|
||||||
|
" <td>Belarus</td>\n",
|
||||||
|
" <td>2009</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>66.0</td>\n",
|
||||||
|
" <td>173.0</td>\n",
|
||||||
|
" <td>208.0</td>\n",
|
||||||
|
" <td>287.0</td>\n",
|
||||||
|
" <td>134.0</td>\n",
|
||||||
|
" <td>54.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>41.0</td>\n",
|
||||||
|
" <td>52.0</td>\n",
|
||||||
|
" <td>52.0</td>\n",
|
||||||
|
" <td>41.0</td>\n",
|
||||||
|
" <td>25.0</td>\n",
|
||||||
|
" <td>68.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>7234</th>\n",
|
||||||
|
" <td>Zimbabwe</td>\n",
|
||||||
|
" <td>2007</td>\n",
|
||||||
|
" <td>138.0</td>\n",
|
||||||
|
" <td>500.0</td>\n",
|
||||||
|
" <td>3693.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>716.0</td>\n",
|
||||||
|
" <td>292.0</td>\n",
|
||||||
|
" <td>153.0</td>\n",
|
||||||
|
" <td>185.0</td>\n",
|
||||||
|
" <td>739.0</td>\n",
|
||||||
|
" <td>3311.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>553.0</td>\n",
|
||||||
|
" <td>213.0</td>\n",
|
||||||
|
" <td>90.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3471</th>\n",
|
||||||
|
" <td>Kuwait</td>\n",
|
||||||
|
" <td>2008</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>18.0</td>\n",
|
||||||
|
" <td>90.0</td>\n",
|
||||||
|
" <td>56.0</td>\n",
|
||||||
|
" <td>34.0</td>\n",
|
||||||
|
" <td>11.0</td>\n",
|
||||||
|
" <td>9.0</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>33.0</td>\n",
|
||||||
|
" <td>47.0</td>\n",
|
||||||
|
" <td>27.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>5.0</td>\n",
|
||||||
|
" <td>6.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3336</th>\n",
|
||||||
|
" <td>Jordan</td>\n",
|
||||||
|
" <td>2009</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>5.0</td>\n",
|
||||||
|
" <td>15.0</td>\n",
|
||||||
|
" <td>14.0</td>\n",
|
||||||
|
" <td>10.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>6.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>14.0</td>\n",
|
||||||
|
" <td>8.0</td>\n",
|
||||||
|
" <td>3.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>12.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2689</th>\n",
|
||||||
|
" <td>Grenada</td>\n",
|
||||||
|
" <td>2008</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</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>634</th>\n",
|
||||||
|
" <td>Belarus</td>\n",
|
||||||
|
" <td>2001</td>\n",
|
||||||
|
" <td>2.0</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>4.0</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",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" country year sp_m_014 sp_m_1524 sp_m_2534 sp_m_3544 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"5551 San Marino 2009 NaN NaN NaN NaN \n",
|
||||||
|
"642 Belarus 2009 0.0 66.0 173.0 208.0 \n",
|
||||||
|
"7234 Zimbabwe 2007 138.0 500.0 3693.0 0.0 \n",
|
||||||
|
"3471 Kuwait 2008 0.0 18.0 90.0 56.0 \n",
|
||||||
|
"3336 Jordan 2009 1.0 5.0 15.0 14.0 \n",
|
||||||
|
"2689 Grenada 2008 NaN 1.0 NaN 1.0 \n",
|
||||||
|
"634 Belarus 2001 2.0 NaN NaN NaN \n",
|
||||||
|
"\n",
|
||||||
|
" sp_m_4554 sp_m_5564 sp_m_65 sp_f_014 sp_f_1524 sp_f_2534 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"5551 NaN NaN NaN NaN NaN NaN \n",
|
||||||
|
"642 287.0 134.0 54.0 0.0 41.0 52.0 \n",
|
||||||
|
"7234 716.0 292.0 153.0 185.0 739.0 3311.0 \n",
|
||||||
|
"3471 34.0 11.0 9.0 2.0 33.0 47.0 \n",
|
||||||
|
"3336 10.0 7.0 6.0 0.0 7.0 14.0 \n",
|
||||||
|
"2689 2.0 NaN 1.0 NaN NaN NaN \n",
|
||||||
|
"634 NaN NaN NaN 4.0 NaN NaN \n",
|
||||||
|
"\n",
|
||||||
|
" sp_f_3544 sp_f_4554 sp_f_5564 sp_f_65 \n",
|
||||||
|
"rownames \n",
|
||||||
|
"5551 NaN NaN NaN NaN \n",
|
||||||
|
"642 52.0 41.0 25.0 68.0 \n",
|
||||||
|
"7234 0.0 553.0 213.0 90.0 \n",
|
||||||
|
"3471 27.0 7.0 5.0 6.0 \n",
|
||||||
|
"3336 8.0 3.0 7.0 12.0 \n",
|
||||||
|
"2689 NaN NaN NaN NaN \n",
|
||||||
|
"634 NaN NaN NaN NaN "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw.sample(7, random_state=727)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "6e8b1d89",
|
||||||
|
"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>country</th>\n",
|
||||||
|
" <th>year</th>\n",
|
||||||
|
" <th>sp_m_014</th>\n",
|
||||||
|
" <th>sp_m_1524</th>\n",
|
||||||
|
" <th>sp_m_2534</th>\n",
|
||||||
|
" <th>sp_m_3544</th>\n",
|
||||||
|
" <th>sp_m_4554</th>\n",
|
||||||
|
" <th>sp_m_5564</th>\n",
|
||||||
|
" <th>sp_m_65</th>\n",
|
||||||
|
" <th>sp_f_014</th>\n",
|
||||||
|
" <th>sp_f_1524</th>\n",
|
||||||
|
" <th>sp_f_2534</th>\n",
|
||||||
|
" <th>sp_f_3544</th>\n",
|
||||||
|
" <th>sp_f_4554</th>\n",
|
||||||
|
" <th>sp_f_5564</th>\n",
|
||||||
|
" <th>sp_f_65</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>rownames</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>191</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>186.0</td>\n",
|
||||||
|
" <td>999.0</td>\n",
|
||||||
|
" <td>1003.0</td>\n",
|
||||||
|
" <td>912.0</td>\n",
|
||||||
|
" <td>482.0</td>\n",
|
||||||
|
" <td>312.0</td>\n",
|
||||||
|
" <td>194.0</td>\n",
|
||||||
|
" <td>247.0</td>\n",
|
||||||
|
" <td>1142.0</td>\n",
|
||||||
|
" <td>1091.0</td>\n",
|
||||||
|
" <td>844.0</td>\n",
|
||||||
|
" <td>417.0</td>\n",
|
||||||
|
" <td>200.0</td>\n",
|
||||||
|
" <td>120.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>192</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2001</td>\n",
|
||||||
|
" <td>230.0</td>\n",
|
||||||
|
" <td>892.0</td>\n",
|
||||||
|
" <td>752.0</td>\n",
|
||||||
|
" <td>648.0</td>\n",
|
||||||
|
" <td>420.0</td>\n",
|
||||||
|
" <td>197.0</td>\n",
|
||||||
|
" <td>173.0</td>\n",
|
||||||
|
" <td>279.0</td>\n",
|
||||||
|
" <td>993.0</td>\n",
|
||||||
|
" <td>869.0</td>\n",
|
||||||
|
" <td>647.0</td>\n",
|
||||||
|
" <td>323.0</td>\n",
|
||||||
|
" <td>200.0</td>\n",
|
||||||
|
" <td>182.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>193</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2002</td>\n",
|
||||||
|
" <td>435.0</td>\n",
|
||||||
|
" <td>2223.0</td>\n",
|
||||||
|
" <td>2292.0</td>\n",
|
||||||
|
" <td>1915.0</td>\n",
|
||||||
|
" <td>1187.0</td>\n",
|
||||||
|
" <td>624.0</td>\n",
|
||||||
|
" <td>444.0</td>\n",
|
||||||
|
" <td>640.0</td>\n",
|
||||||
|
" <td>2610.0</td>\n",
|
||||||
|
" <td>2208.0</td>\n",
|
||||||
|
" <td>1600.0</td>\n",
|
||||||
|
" <td>972.0</td>\n",
|
||||||
|
" <td>533.0</td>\n",
|
||||||
|
" <td>305.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>194</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2003</td>\n",
|
||||||
|
" <td>409.0</td>\n",
|
||||||
|
" <td>2355.0</td>\n",
|
||||||
|
" <td>2598.0</td>\n",
|
||||||
|
" <td>1908.0</td>\n",
|
||||||
|
" <td>1090.0</td>\n",
|
||||||
|
" <td>512.0</td>\n",
|
||||||
|
" <td>361.0</td>\n",
|
||||||
|
" <td>591.0</td>\n",
|
||||||
|
" <td>3078.0</td>\n",
|
||||||
|
" <td>2641.0</td>\n",
|
||||||
|
" <td>1747.0</td>\n",
|
||||||
|
" <td>1157.0</td>\n",
|
||||||
|
" <td>395.0</td>\n",
|
||||||
|
" <td>129.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>195</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2004</td>\n",
|
||||||
|
" <td>554.0</td>\n",
|
||||||
|
" <td>2684.0</td>\n",
|
||||||
|
" <td>2659.0</td>\n",
|
||||||
|
" <td>1998.0</td>\n",
|
||||||
|
" <td>1196.0</td>\n",
|
||||||
|
" <td>561.0</td>\n",
|
||||||
|
" <td>321.0</td>\n",
|
||||||
|
" <td>733.0</td>\n",
|
||||||
|
" <td>3198.0</td>\n",
|
||||||
|
" <td>2772.0</td>\n",
|
||||||
|
" <td>1854.0</td>\n",
|
||||||
|
" <td>1029.0</td>\n",
|
||||||
|
" <td>505.0</td>\n",
|
||||||
|
" <td>269.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>196</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2005</td>\n",
|
||||||
|
" <td>520.0</td>\n",
|
||||||
|
" <td>2549.0</td>\n",
|
||||||
|
" <td>2797.0</td>\n",
|
||||||
|
" <td>1918.0</td>\n",
|
||||||
|
" <td>1255.0</td>\n",
|
||||||
|
" <td>665.0</td>\n",
|
||||||
|
" <td>461.0</td>\n",
|
||||||
|
" <td>704.0</td>\n",
|
||||||
|
" <td>2926.0</td>\n",
|
||||||
|
" <td>2682.0</td>\n",
|
||||||
|
" <td>1797.0</td>\n",
|
||||||
|
" <td>1138.0</td>\n",
|
||||||
|
" <td>581.0</td>\n",
|
||||||
|
" <td>417.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>197</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2006</td>\n",
|
||||||
|
" <td>540.0</td>\n",
|
||||||
|
" <td>2632.0</td>\n",
|
||||||
|
" <td>3049.0</td>\n",
|
||||||
|
" <td>2182.0</td>\n",
|
||||||
|
" <td>1397.0</td>\n",
|
||||||
|
" <td>729.0</td>\n",
|
||||||
|
" <td>428.0</td>\n",
|
||||||
|
" <td>689.0</td>\n",
|
||||||
|
" <td>2851.0</td>\n",
|
||||||
|
" <td>2892.0</td>\n",
|
||||||
|
" <td>1990.0</td>\n",
|
||||||
|
" <td>1223.0</td>\n",
|
||||||
|
" <td>583.0</td>\n",
|
||||||
|
" <td>314.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>198</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2007</td>\n",
|
||||||
|
" <td>484.0</td>\n",
|
||||||
|
" <td>2824.0</td>\n",
|
||||||
|
" <td>3197.0</td>\n",
|
||||||
|
" <td>2255.0</td>\n",
|
||||||
|
" <td>1357.0</td>\n",
|
||||||
|
" <td>699.0</td>\n",
|
||||||
|
" <td>465.0</td>\n",
|
||||||
|
" <td>703.0</td>\n",
|
||||||
|
" <td>2943.0</td>\n",
|
||||||
|
" <td>2721.0</td>\n",
|
||||||
|
" <td>1812.0</td>\n",
|
||||||
|
" <td>1041.0</td>\n",
|
||||||
|
" <td>554.0</td>\n",
|
||||||
|
" <td>367.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>199</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2008</td>\n",
|
||||||
|
" <td>367.0</td>\n",
|
||||||
|
" <td>2970.0</td>\n",
|
||||||
|
" <td>3493.0</td>\n",
|
||||||
|
" <td>2418.0</td>\n",
|
||||||
|
" <td>1480.0</td>\n",
|
||||||
|
" <td>733.0</td>\n",
|
||||||
|
" <td>420.0</td>\n",
|
||||||
|
" <td>512.0</td>\n",
|
||||||
|
" <td>3199.0</td>\n",
|
||||||
|
" <td>2786.0</td>\n",
|
||||||
|
" <td>2082.0</td>\n",
|
||||||
|
" <td>1209.0</td>\n",
|
||||||
|
" <td>556.0</td>\n",
|
||||||
|
" <td>337.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>200</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2009</td>\n",
|
||||||
|
" <td>392.0</td>\n",
|
||||||
|
" <td>3054.0</td>\n",
|
||||||
|
" <td>3600.0</td>\n",
|
||||||
|
" <td>2420.0</td>\n",
|
||||||
|
" <td>1590.0</td>\n",
|
||||||
|
" <td>748.0</td>\n",
|
||||||
|
" <td>463.0</td>\n",
|
||||||
|
" <td>568.0</td>\n",
|
||||||
|
" <td>3152.0</td>\n",
|
||||||
|
" <td>2798.0</td>\n",
|
||||||
|
" <td>1790.0</td>\n",
|
||||||
|
" <td>1069.0</td>\n",
|
||||||
|
" <td>572.0</td>\n",
|
||||||
|
" <td>272.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>201</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2010</td>\n",
|
||||||
|
" <td>448.0</td>\n",
|
||||||
|
" <td>2900.0</td>\n",
|
||||||
|
" <td>3584.0</td>\n",
|
||||||
|
" <td>2415.0</td>\n",
|
||||||
|
" <td>1424.0</td>\n",
|
||||||
|
" <td>691.0</td>\n",
|
||||||
|
" <td>355.0</td>\n",
|
||||||
|
" <td>558.0</td>\n",
|
||||||
|
" <td>2763.0</td>\n",
|
||||||
|
" <td>2594.0</td>\n",
|
||||||
|
" <td>1688.0</td>\n",
|
||||||
|
" <td>958.0</td>\n",
|
||||||
|
" <td>482.0</td>\n",
|
||||||
|
" <td>286.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>202</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2011</td>\n",
|
||||||
|
" <td>501.0</td>\n",
|
||||||
|
" <td>3000.0</td>\n",
|
||||||
|
" <td>3792.0</td>\n",
|
||||||
|
" <td>2386.0</td>\n",
|
||||||
|
" <td>1395.0</td>\n",
|
||||||
|
" <td>680.0</td>\n",
|
||||||
|
" <td>455.0</td>\n",
|
||||||
|
" <td>708.0</td>\n",
|
||||||
|
" <td>2731.0</td>\n",
|
||||||
|
" <td>2563.0</td>\n",
|
||||||
|
" <td>1683.0</td>\n",
|
||||||
|
" <td>1006.0</td>\n",
|
||||||
|
" <td>457.0</td>\n",
|
||||||
|
" <td>346.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>203</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2012</td>\n",
|
||||||
|
" <td>390.0</td>\n",
|
||||||
|
" <td>2804.0</td>\n",
|
||||||
|
" <td>3627.0</td>\n",
|
||||||
|
" <td>2529.0</td>\n",
|
||||||
|
" <td>1427.0</td>\n",
|
||||||
|
" <td>732.0</td>\n",
|
||||||
|
" <td>424.0</td>\n",
|
||||||
|
" <td>592.0</td>\n",
|
||||||
|
" <td>2501.0</td>\n",
|
||||||
|
" <td>2540.0</td>\n",
|
||||||
|
" <td>1617.0</td>\n",
|
||||||
|
" <td>1028.0</td>\n",
|
||||||
|
" <td>529.0</td>\n",
|
||||||
|
" <td>384.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" country year sp_m_014 sp_m_1524 sp_m_2534 sp_m_3544 sp_m_4554 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"191 Angola 2000 186.0 999.0 1003.0 912.0 482.0 \n",
|
||||||
|
"192 Angola 2001 230.0 892.0 752.0 648.0 420.0 \n",
|
||||||
|
"193 Angola 2002 435.0 2223.0 2292.0 1915.0 1187.0 \n",
|
||||||
|
"194 Angola 2003 409.0 2355.0 2598.0 1908.0 1090.0 \n",
|
||||||
|
"195 Angola 2004 554.0 2684.0 2659.0 1998.0 1196.0 \n",
|
||||||
|
"196 Angola 2005 520.0 2549.0 2797.0 1918.0 1255.0 \n",
|
||||||
|
"197 Angola 2006 540.0 2632.0 3049.0 2182.0 1397.0 \n",
|
||||||
|
"198 Angola 2007 484.0 2824.0 3197.0 2255.0 1357.0 \n",
|
||||||
|
"199 Angola 2008 367.0 2970.0 3493.0 2418.0 1480.0 \n",
|
||||||
|
"200 Angola 2009 392.0 3054.0 3600.0 2420.0 1590.0 \n",
|
||||||
|
"201 Angola 2010 448.0 2900.0 3584.0 2415.0 1424.0 \n",
|
||||||
|
"202 Angola 2011 501.0 3000.0 3792.0 2386.0 1395.0 \n",
|
||||||
|
"203 Angola 2012 390.0 2804.0 3627.0 2529.0 1427.0 \n",
|
||||||
|
"\n",
|
||||||
|
" sp_m_5564 sp_m_65 sp_f_014 sp_f_1524 sp_f_2534 sp_f_3544 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"191 312.0 194.0 247.0 1142.0 1091.0 844.0 \n",
|
||||||
|
"192 197.0 173.0 279.0 993.0 869.0 647.0 \n",
|
||||||
|
"193 624.0 444.0 640.0 2610.0 2208.0 1600.0 \n",
|
||||||
|
"194 512.0 361.0 591.0 3078.0 2641.0 1747.0 \n",
|
||||||
|
"195 561.0 321.0 733.0 3198.0 2772.0 1854.0 \n",
|
||||||
|
"196 665.0 461.0 704.0 2926.0 2682.0 1797.0 \n",
|
||||||
|
"197 729.0 428.0 689.0 2851.0 2892.0 1990.0 \n",
|
||||||
|
"198 699.0 465.0 703.0 2943.0 2721.0 1812.0 \n",
|
||||||
|
"199 733.0 420.0 512.0 3199.0 2786.0 2082.0 \n",
|
||||||
|
"200 748.0 463.0 568.0 3152.0 2798.0 1790.0 \n",
|
||||||
|
"201 691.0 355.0 558.0 2763.0 2594.0 1688.0 \n",
|
||||||
|
"202 680.0 455.0 708.0 2731.0 2563.0 1683.0 \n",
|
||||||
|
"203 732.0 424.0 592.0 2501.0 2540.0 1617.0 \n",
|
||||||
|
"\n",
|
||||||
|
" sp_f_4554 sp_f_5564 sp_f_65 \n",
|
||||||
|
"rownames \n",
|
||||||
|
"191 417.0 200.0 120.0 \n",
|
||||||
|
"192 323.0 200.0 182.0 \n",
|
||||||
|
"193 972.0 533.0 305.0 \n",
|
||||||
|
"194 1157.0 395.0 129.0 \n",
|
||||||
|
"195 1029.0 505.0 269.0 \n",
|
||||||
|
"196 1138.0 581.0 417.0 \n",
|
||||||
|
"197 1223.0 583.0 314.0 \n",
|
||||||
|
"198 1041.0 554.0 367.0 \n",
|
||||||
|
"199 1209.0 556.0 337.0 \n",
|
||||||
|
"200 1069.0 572.0 272.0 \n",
|
||||||
|
"201 958.0 482.0 286.0 \n",
|
||||||
|
"202 1006.0 457.0 346.0 \n",
|
||||||
|
"203 1028.0 529.0 384.0 "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw[tb_raw['country'] == 'Angola']"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "116c47ad",
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Index(['country', 'year', 'sp_m_014', 'sp_m_1524', 'sp_m_2534', 'sp_m_3544',\n",
|
||||||
|
" 'sp_m_4554', 'sp_m_5564', 'sp_m_65', 'sp_f_014', 'sp_f_1524',\n",
|
||||||
|
" 'sp_f_2534', 'sp_f_3544', 'sp_f_4554', 'sp_f_5564', 'sp_f_65'],\n",
|
||||||
|
" dtype='object')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw.columns"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "062ed46a",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 1. Make data tidy\n",
|
||||||
|
"\n",
|
||||||
|
"The final table should have these columns: `country`, `year`, `gender`, `age_range`, `cases`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "568c8440",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9d1f036e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# 2. Compute summary tables\n",
|
||||||
|
"\n",
|
||||||
|
"1. Compute the number of cases per country and gender, for data between 2000 and 2006 (included)\n",
|
||||||
|
"2. Compute the number of cases per country and year range (2000-2006, 2007-2012) on rows, and gender on columns"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "c8e9b0e4",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.11.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
13410
exercises/tabular_tidy_data/tidy_data_solution.ipynb
Normal file
13410
exercises/tabular_tidy_data/tidy_data_solution.ipynb
Normal file
File diff suppressed because it is too large
Load diff
7241
exercises/tabular_tidy_data/who2.csv
Normal file
7241
exercises/tabular_tidy_data/who2.csv
Normal file
File diff suppressed because it is too large
Load diff
776
exercises/tabular_tuberculosis/tuberculosis.ipynb
Normal file
776
exercises/tabular_tuberculosis/tuberculosis.ipynb
Normal file
|
@ -0,0 +1,776 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "e951a26e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Exercise: Analysis of tuberculosis cases by country and year period\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "6b181870",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"\n",
|
||||||
|
"pd.set_option('display.max_rows', 1000)\n",
|
||||||
|
"pd.set_option('display.max_columns', 100)\n",
|
||||||
|
"pd.set_option(\"display.max_colwidth\", None)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9adcc036",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load the tuberculosis data from the World Health Organization"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "5d9e9162",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"tb_raw = pd.read_csv('who2.csv', index_col='rownames')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "cf7691e5",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Only keep data between 2000 and 2012"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "a953d230",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"cols = ['country', 'year'] + [c for c in tb_raw.columns if c.startswith('sp')]\n",
|
||||||
|
"tb_raw = tb_raw.loc[tb_raw['year'].between(2000, 2012), cols]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "ba962fb7",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"(2783, 16)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw.shape"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "c79a5b8d",
|
||||||
|
"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>country</th>\n",
|
||||||
|
" <th>year</th>\n",
|
||||||
|
" <th>sp_m_014</th>\n",
|
||||||
|
" <th>sp_m_1524</th>\n",
|
||||||
|
" <th>sp_m_2534</th>\n",
|
||||||
|
" <th>sp_m_3544</th>\n",
|
||||||
|
" <th>sp_m_4554</th>\n",
|
||||||
|
" <th>sp_m_5564</th>\n",
|
||||||
|
" <th>sp_m_65</th>\n",
|
||||||
|
" <th>sp_f_014</th>\n",
|
||||||
|
" <th>sp_f_1524</th>\n",
|
||||||
|
" <th>sp_f_2534</th>\n",
|
||||||
|
" <th>sp_f_3544</th>\n",
|
||||||
|
" <th>sp_f_4554</th>\n",
|
||||||
|
" <th>sp_f_5564</th>\n",
|
||||||
|
" <th>sp_f_65</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>rownames</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>5551</th>\n",
|
||||||
|
" <td>San Marino</td>\n",
|
||||||
|
" <td>2009</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",
|
||||||
|
" <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>642</th>\n",
|
||||||
|
" <td>Belarus</td>\n",
|
||||||
|
" <td>2009</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>66.0</td>\n",
|
||||||
|
" <td>173.0</td>\n",
|
||||||
|
" <td>208.0</td>\n",
|
||||||
|
" <td>287.0</td>\n",
|
||||||
|
" <td>134.0</td>\n",
|
||||||
|
" <td>54.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>41.0</td>\n",
|
||||||
|
" <td>52.0</td>\n",
|
||||||
|
" <td>52.0</td>\n",
|
||||||
|
" <td>41.0</td>\n",
|
||||||
|
" <td>25.0</td>\n",
|
||||||
|
" <td>68.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>7234</th>\n",
|
||||||
|
" <td>Zimbabwe</td>\n",
|
||||||
|
" <td>2007</td>\n",
|
||||||
|
" <td>138.0</td>\n",
|
||||||
|
" <td>500.0</td>\n",
|
||||||
|
" <td>3693.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>716.0</td>\n",
|
||||||
|
" <td>292.0</td>\n",
|
||||||
|
" <td>153.0</td>\n",
|
||||||
|
" <td>185.0</td>\n",
|
||||||
|
" <td>739.0</td>\n",
|
||||||
|
" <td>3311.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>553.0</td>\n",
|
||||||
|
" <td>213.0</td>\n",
|
||||||
|
" <td>90.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3471</th>\n",
|
||||||
|
" <td>Kuwait</td>\n",
|
||||||
|
" <td>2008</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>18.0</td>\n",
|
||||||
|
" <td>90.0</td>\n",
|
||||||
|
" <td>56.0</td>\n",
|
||||||
|
" <td>34.0</td>\n",
|
||||||
|
" <td>11.0</td>\n",
|
||||||
|
" <td>9.0</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>33.0</td>\n",
|
||||||
|
" <td>47.0</td>\n",
|
||||||
|
" <td>27.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>5.0</td>\n",
|
||||||
|
" <td>6.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3336</th>\n",
|
||||||
|
" <td>Jordan</td>\n",
|
||||||
|
" <td>2009</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>5.0</td>\n",
|
||||||
|
" <td>15.0</td>\n",
|
||||||
|
" <td>14.0</td>\n",
|
||||||
|
" <td>10.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>6.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>14.0</td>\n",
|
||||||
|
" <td>8.0</td>\n",
|
||||||
|
" <td>3.0</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>12.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2689</th>\n",
|
||||||
|
" <td>Grenada</td>\n",
|
||||||
|
" <td>2008</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</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>634</th>\n",
|
||||||
|
" <td>Belarus</td>\n",
|
||||||
|
" <td>2001</td>\n",
|
||||||
|
" <td>2.0</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>4.0</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",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" country year sp_m_014 sp_m_1524 sp_m_2534 sp_m_3544 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"5551 San Marino 2009 NaN NaN NaN NaN \n",
|
||||||
|
"642 Belarus 2009 0.0 66.0 173.0 208.0 \n",
|
||||||
|
"7234 Zimbabwe 2007 138.0 500.0 3693.0 0.0 \n",
|
||||||
|
"3471 Kuwait 2008 0.0 18.0 90.0 56.0 \n",
|
||||||
|
"3336 Jordan 2009 1.0 5.0 15.0 14.0 \n",
|
||||||
|
"2689 Grenada 2008 NaN 1.0 NaN 1.0 \n",
|
||||||
|
"634 Belarus 2001 2.0 NaN NaN NaN \n",
|
||||||
|
"\n",
|
||||||
|
" sp_m_4554 sp_m_5564 sp_m_65 sp_f_014 sp_f_1524 sp_f_2534 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"5551 NaN NaN NaN NaN NaN NaN \n",
|
||||||
|
"642 287.0 134.0 54.0 0.0 41.0 52.0 \n",
|
||||||
|
"7234 716.0 292.0 153.0 185.0 739.0 3311.0 \n",
|
||||||
|
"3471 34.0 11.0 9.0 2.0 33.0 47.0 \n",
|
||||||
|
"3336 10.0 7.0 6.0 0.0 7.0 14.0 \n",
|
||||||
|
"2689 2.0 NaN 1.0 NaN NaN NaN \n",
|
||||||
|
"634 NaN NaN NaN 4.0 NaN NaN \n",
|
||||||
|
"\n",
|
||||||
|
" sp_f_3544 sp_f_4554 sp_f_5564 sp_f_65 \n",
|
||||||
|
"rownames \n",
|
||||||
|
"5551 NaN NaN NaN NaN \n",
|
||||||
|
"642 52.0 41.0 25.0 68.0 \n",
|
||||||
|
"7234 0.0 553.0 213.0 90.0 \n",
|
||||||
|
"3471 27.0 7.0 5.0 6.0 \n",
|
||||||
|
"3336 8.0 3.0 7.0 12.0 \n",
|
||||||
|
"2689 NaN NaN NaN NaN \n",
|
||||||
|
"634 NaN NaN NaN NaN "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw.sample(7, random_state=727)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "6e8b1d89",
|
||||||
|
"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>country</th>\n",
|
||||||
|
" <th>year</th>\n",
|
||||||
|
" <th>sp_m_014</th>\n",
|
||||||
|
" <th>sp_m_1524</th>\n",
|
||||||
|
" <th>sp_m_2534</th>\n",
|
||||||
|
" <th>sp_m_3544</th>\n",
|
||||||
|
" <th>sp_m_4554</th>\n",
|
||||||
|
" <th>sp_m_5564</th>\n",
|
||||||
|
" <th>sp_m_65</th>\n",
|
||||||
|
" <th>sp_f_014</th>\n",
|
||||||
|
" <th>sp_f_1524</th>\n",
|
||||||
|
" <th>sp_f_2534</th>\n",
|
||||||
|
" <th>sp_f_3544</th>\n",
|
||||||
|
" <th>sp_f_4554</th>\n",
|
||||||
|
" <th>sp_f_5564</th>\n",
|
||||||
|
" <th>sp_f_65</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>rownames</th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>191</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>186.0</td>\n",
|
||||||
|
" <td>999.0</td>\n",
|
||||||
|
" <td>1003.0</td>\n",
|
||||||
|
" <td>912.0</td>\n",
|
||||||
|
" <td>482.0</td>\n",
|
||||||
|
" <td>312.0</td>\n",
|
||||||
|
" <td>194.0</td>\n",
|
||||||
|
" <td>247.0</td>\n",
|
||||||
|
" <td>1142.0</td>\n",
|
||||||
|
" <td>1091.0</td>\n",
|
||||||
|
" <td>844.0</td>\n",
|
||||||
|
" <td>417.0</td>\n",
|
||||||
|
" <td>200.0</td>\n",
|
||||||
|
" <td>120.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>192</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2001</td>\n",
|
||||||
|
" <td>230.0</td>\n",
|
||||||
|
" <td>892.0</td>\n",
|
||||||
|
" <td>752.0</td>\n",
|
||||||
|
" <td>648.0</td>\n",
|
||||||
|
" <td>420.0</td>\n",
|
||||||
|
" <td>197.0</td>\n",
|
||||||
|
" <td>173.0</td>\n",
|
||||||
|
" <td>279.0</td>\n",
|
||||||
|
" <td>993.0</td>\n",
|
||||||
|
" <td>869.0</td>\n",
|
||||||
|
" <td>647.0</td>\n",
|
||||||
|
" <td>323.0</td>\n",
|
||||||
|
" <td>200.0</td>\n",
|
||||||
|
" <td>182.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>193</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2002</td>\n",
|
||||||
|
" <td>435.0</td>\n",
|
||||||
|
" <td>2223.0</td>\n",
|
||||||
|
" <td>2292.0</td>\n",
|
||||||
|
" <td>1915.0</td>\n",
|
||||||
|
" <td>1187.0</td>\n",
|
||||||
|
" <td>624.0</td>\n",
|
||||||
|
" <td>444.0</td>\n",
|
||||||
|
" <td>640.0</td>\n",
|
||||||
|
" <td>2610.0</td>\n",
|
||||||
|
" <td>2208.0</td>\n",
|
||||||
|
" <td>1600.0</td>\n",
|
||||||
|
" <td>972.0</td>\n",
|
||||||
|
" <td>533.0</td>\n",
|
||||||
|
" <td>305.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>194</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2003</td>\n",
|
||||||
|
" <td>409.0</td>\n",
|
||||||
|
" <td>2355.0</td>\n",
|
||||||
|
" <td>2598.0</td>\n",
|
||||||
|
" <td>1908.0</td>\n",
|
||||||
|
" <td>1090.0</td>\n",
|
||||||
|
" <td>512.0</td>\n",
|
||||||
|
" <td>361.0</td>\n",
|
||||||
|
" <td>591.0</td>\n",
|
||||||
|
" <td>3078.0</td>\n",
|
||||||
|
" <td>2641.0</td>\n",
|
||||||
|
" <td>1747.0</td>\n",
|
||||||
|
" <td>1157.0</td>\n",
|
||||||
|
" <td>395.0</td>\n",
|
||||||
|
" <td>129.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>195</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2004</td>\n",
|
||||||
|
" <td>554.0</td>\n",
|
||||||
|
" <td>2684.0</td>\n",
|
||||||
|
" <td>2659.0</td>\n",
|
||||||
|
" <td>1998.0</td>\n",
|
||||||
|
" <td>1196.0</td>\n",
|
||||||
|
" <td>561.0</td>\n",
|
||||||
|
" <td>321.0</td>\n",
|
||||||
|
" <td>733.0</td>\n",
|
||||||
|
" <td>3198.0</td>\n",
|
||||||
|
" <td>2772.0</td>\n",
|
||||||
|
" <td>1854.0</td>\n",
|
||||||
|
" <td>1029.0</td>\n",
|
||||||
|
" <td>505.0</td>\n",
|
||||||
|
" <td>269.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>196</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2005</td>\n",
|
||||||
|
" <td>520.0</td>\n",
|
||||||
|
" <td>2549.0</td>\n",
|
||||||
|
" <td>2797.0</td>\n",
|
||||||
|
" <td>1918.0</td>\n",
|
||||||
|
" <td>1255.0</td>\n",
|
||||||
|
" <td>665.0</td>\n",
|
||||||
|
" <td>461.0</td>\n",
|
||||||
|
" <td>704.0</td>\n",
|
||||||
|
" <td>2926.0</td>\n",
|
||||||
|
" <td>2682.0</td>\n",
|
||||||
|
" <td>1797.0</td>\n",
|
||||||
|
" <td>1138.0</td>\n",
|
||||||
|
" <td>581.0</td>\n",
|
||||||
|
" <td>417.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>197</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2006</td>\n",
|
||||||
|
" <td>540.0</td>\n",
|
||||||
|
" <td>2632.0</td>\n",
|
||||||
|
" <td>3049.0</td>\n",
|
||||||
|
" <td>2182.0</td>\n",
|
||||||
|
" <td>1397.0</td>\n",
|
||||||
|
" <td>729.0</td>\n",
|
||||||
|
" <td>428.0</td>\n",
|
||||||
|
" <td>689.0</td>\n",
|
||||||
|
" <td>2851.0</td>\n",
|
||||||
|
" <td>2892.0</td>\n",
|
||||||
|
" <td>1990.0</td>\n",
|
||||||
|
" <td>1223.0</td>\n",
|
||||||
|
" <td>583.0</td>\n",
|
||||||
|
" <td>314.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>198</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2007</td>\n",
|
||||||
|
" <td>484.0</td>\n",
|
||||||
|
" <td>2824.0</td>\n",
|
||||||
|
" <td>3197.0</td>\n",
|
||||||
|
" <td>2255.0</td>\n",
|
||||||
|
" <td>1357.0</td>\n",
|
||||||
|
" <td>699.0</td>\n",
|
||||||
|
" <td>465.0</td>\n",
|
||||||
|
" <td>703.0</td>\n",
|
||||||
|
" <td>2943.0</td>\n",
|
||||||
|
" <td>2721.0</td>\n",
|
||||||
|
" <td>1812.0</td>\n",
|
||||||
|
" <td>1041.0</td>\n",
|
||||||
|
" <td>554.0</td>\n",
|
||||||
|
" <td>367.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>199</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2008</td>\n",
|
||||||
|
" <td>367.0</td>\n",
|
||||||
|
" <td>2970.0</td>\n",
|
||||||
|
" <td>3493.0</td>\n",
|
||||||
|
" <td>2418.0</td>\n",
|
||||||
|
" <td>1480.0</td>\n",
|
||||||
|
" <td>733.0</td>\n",
|
||||||
|
" <td>420.0</td>\n",
|
||||||
|
" <td>512.0</td>\n",
|
||||||
|
" <td>3199.0</td>\n",
|
||||||
|
" <td>2786.0</td>\n",
|
||||||
|
" <td>2082.0</td>\n",
|
||||||
|
" <td>1209.0</td>\n",
|
||||||
|
" <td>556.0</td>\n",
|
||||||
|
" <td>337.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>200</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2009</td>\n",
|
||||||
|
" <td>392.0</td>\n",
|
||||||
|
" <td>3054.0</td>\n",
|
||||||
|
" <td>3600.0</td>\n",
|
||||||
|
" <td>2420.0</td>\n",
|
||||||
|
" <td>1590.0</td>\n",
|
||||||
|
" <td>748.0</td>\n",
|
||||||
|
" <td>463.0</td>\n",
|
||||||
|
" <td>568.0</td>\n",
|
||||||
|
" <td>3152.0</td>\n",
|
||||||
|
" <td>2798.0</td>\n",
|
||||||
|
" <td>1790.0</td>\n",
|
||||||
|
" <td>1069.0</td>\n",
|
||||||
|
" <td>572.0</td>\n",
|
||||||
|
" <td>272.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>201</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2010</td>\n",
|
||||||
|
" <td>448.0</td>\n",
|
||||||
|
" <td>2900.0</td>\n",
|
||||||
|
" <td>3584.0</td>\n",
|
||||||
|
" <td>2415.0</td>\n",
|
||||||
|
" <td>1424.0</td>\n",
|
||||||
|
" <td>691.0</td>\n",
|
||||||
|
" <td>355.0</td>\n",
|
||||||
|
" <td>558.0</td>\n",
|
||||||
|
" <td>2763.0</td>\n",
|
||||||
|
" <td>2594.0</td>\n",
|
||||||
|
" <td>1688.0</td>\n",
|
||||||
|
" <td>958.0</td>\n",
|
||||||
|
" <td>482.0</td>\n",
|
||||||
|
" <td>286.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>202</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2011</td>\n",
|
||||||
|
" <td>501.0</td>\n",
|
||||||
|
" <td>3000.0</td>\n",
|
||||||
|
" <td>3792.0</td>\n",
|
||||||
|
" <td>2386.0</td>\n",
|
||||||
|
" <td>1395.0</td>\n",
|
||||||
|
" <td>680.0</td>\n",
|
||||||
|
" <td>455.0</td>\n",
|
||||||
|
" <td>708.0</td>\n",
|
||||||
|
" <td>2731.0</td>\n",
|
||||||
|
" <td>2563.0</td>\n",
|
||||||
|
" <td>1683.0</td>\n",
|
||||||
|
" <td>1006.0</td>\n",
|
||||||
|
" <td>457.0</td>\n",
|
||||||
|
" <td>346.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>203</th>\n",
|
||||||
|
" <td>Angola</td>\n",
|
||||||
|
" <td>2012</td>\n",
|
||||||
|
" <td>390.0</td>\n",
|
||||||
|
" <td>2804.0</td>\n",
|
||||||
|
" <td>3627.0</td>\n",
|
||||||
|
" <td>2529.0</td>\n",
|
||||||
|
" <td>1427.0</td>\n",
|
||||||
|
" <td>732.0</td>\n",
|
||||||
|
" <td>424.0</td>\n",
|
||||||
|
" <td>592.0</td>\n",
|
||||||
|
" <td>2501.0</td>\n",
|
||||||
|
" <td>2540.0</td>\n",
|
||||||
|
" <td>1617.0</td>\n",
|
||||||
|
" <td>1028.0</td>\n",
|
||||||
|
" <td>529.0</td>\n",
|
||||||
|
" <td>384.0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" country year sp_m_014 sp_m_1524 sp_m_2534 sp_m_3544 sp_m_4554 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"191 Angola 2000 186.0 999.0 1003.0 912.0 482.0 \n",
|
||||||
|
"192 Angola 2001 230.0 892.0 752.0 648.0 420.0 \n",
|
||||||
|
"193 Angola 2002 435.0 2223.0 2292.0 1915.0 1187.0 \n",
|
||||||
|
"194 Angola 2003 409.0 2355.0 2598.0 1908.0 1090.0 \n",
|
||||||
|
"195 Angola 2004 554.0 2684.0 2659.0 1998.0 1196.0 \n",
|
||||||
|
"196 Angola 2005 520.0 2549.0 2797.0 1918.0 1255.0 \n",
|
||||||
|
"197 Angola 2006 540.0 2632.0 3049.0 2182.0 1397.0 \n",
|
||||||
|
"198 Angola 2007 484.0 2824.0 3197.0 2255.0 1357.0 \n",
|
||||||
|
"199 Angola 2008 367.0 2970.0 3493.0 2418.0 1480.0 \n",
|
||||||
|
"200 Angola 2009 392.0 3054.0 3600.0 2420.0 1590.0 \n",
|
||||||
|
"201 Angola 2010 448.0 2900.0 3584.0 2415.0 1424.0 \n",
|
||||||
|
"202 Angola 2011 501.0 3000.0 3792.0 2386.0 1395.0 \n",
|
||||||
|
"203 Angola 2012 390.0 2804.0 3627.0 2529.0 1427.0 \n",
|
||||||
|
"\n",
|
||||||
|
" sp_m_5564 sp_m_65 sp_f_014 sp_f_1524 sp_f_2534 sp_f_3544 \\\n",
|
||||||
|
"rownames \n",
|
||||||
|
"191 312.0 194.0 247.0 1142.0 1091.0 844.0 \n",
|
||||||
|
"192 197.0 173.0 279.0 993.0 869.0 647.0 \n",
|
||||||
|
"193 624.0 444.0 640.0 2610.0 2208.0 1600.0 \n",
|
||||||
|
"194 512.0 361.0 591.0 3078.0 2641.0 1747.0 \n",
|
||||||
|
"195 561.0 321.0 733.0 3198.0 2772.0 1854.0 \n",
|
||||||
|
"196 665.0 461.0 704.0 2926.0 2682.0 1797.0 \n",
|
||||||
|
"197 729.0 428.0 689.0 2851.0 2892.0 1990.0 \n",
|
||||||
|
"198 699.0 465.0 703.0 2943.0 2721.0 1812.0 \n",
|
||||||
|
"199 733.0 420.0 512.0 3199.0 2786.0 2082.0 \n",
|
||||||
|
"200 748.0 463.0 568.0 3152.0 2798.0 1790.0 \n",
|
||||||
|
"201 691.0 355.0 558.0 2763.0 2594.0 1688.0 \n",
|
||||||
|
"202 680.0 455.0 708.0 2731.0 2563.0 1683.0 \n",
|
||||||
|
"203 732.0 424.0 592.0 2501.0 2540.0 1617.0 \n",
|
||||||
|
"\n",
|
||||||
|
" sp_f_4554 sp_f_5564 sp_f_65 \n",
|
||||||
|
"rownames \n",
|
||||||
|
"191 417.0 200.0 120.0 \n",
|
||||||
|
"192 323.0 200.0 182.0 \n",
|
||||||
|
"193 972.0 533.0 305.0 \n",
|
||||||
|
"194 1157.0 395.0 129.0 \n",
|
||||||
|
"195 1029.0 505.0 269.0 \n",
|
||||||
|
"196 1138.0 581.0 417.0 \n",
|
||||||
|
"197 1223.0 583.0 314.0 \n",
|
||||||
|
"198 1041.0 554.0 367.0 \n",
|
||||||
|
"199 1209.0 556.0 337.0 \n",
|
||||||
|
"200 1069.0 572.0 272.0 \n",
|
||||||
|
"201 958.0 482.0 286.0 \n",
|
||||||
|
"202 1006.0 457.0 346.0 \n",
|
||||||
|
"203 1028.0 529.0 384.0 "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw[tb_raw['country'] == 'Angola']"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "116c47ad",
|
||||||
|
"metadata": {
|
||||||
|
"scrolled": true
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Index(['country', 'year', 'sp_m_014', 'sp_m_1524', 'sp_m_2534', 'sp_m_3544',\n",
|
||||||
|
" 'sp_m_4554', 'sp_m_5564', 'sp_m_65', 'sp_f_014', 'sp_f_1524',\n",
|
||||||
|
" 'sp_f_2534', 'sp_f_3544', 'sp_f_4554', 'sp_f_5564', 'sp_f_65'],\n",
|
||||||
|
" dtype='object')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb_raw.columns"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "9d1f036e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Compute summary tables\n",
|
||||||
|
"\n",
|
||||||
|
"1. Compute the number of cases per country and gender"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "51e34c9b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "c8e9b0e4",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.11.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
7241
exercises/tabular_tuberculosis/who2.csv
Normal file
7241
exercises/tabular_tuberculosis/who2.csv
Normal file
File diff suppressed because it is too large
Load diff
6246
exercises/tabular_window_functions/processed_data_predimed.csv
Normal file
6246
exercises/tabular_window_functions/processed_data_predimed.csv
Normal file
File diff suppressed because it is too large
Load diff
467
exercises/tabular_window_functions/window_functions.ipynb
Normal file
467
exercises/tabular_window_functions/window_functions.ipynb
Normal file
|
@ -0,0 +1,467 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "6f6aa857",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Exercise window functions: compute the cumulative number of cases across time, per diet group\n",
|
||||||
|
"\n",
|
||||||
|
"The variable `toevent` contains the time that patients where followed up. We want to calculate the number of events as a function of the follow-up time, separatedely for each diet group. We expect that, if the mediterranean diet has an effect, then over time there will be more cases appearing on the control group in comparison to the other diet groups. \n",
|
||||||
|
"\n",
|
||||||
|
"Here is how to proceed:\n",
|
||||||
|
"- Use a window function to compute the cumulative number of events for each diet group separatedly. As we are interested in the follow-up time, you need to sort the events by the follow-up time first (`toevent`), and then calculate the cumulative sum of events, separatedely per group.\n",
|
||||||
|
"- Add the result as a new column called `'cumulative_event_count'`\n",
|
||||||
|
"\n",
|
||||||
|
"With your new awesome vectorization skills, these two steps should take only one line!\n",
|
||||||
|
"\n",
|
||||||
|
"When ready, execute the code at the end, which has already code that creates a visualiation with the cumulative number of events per group, as a function of the time of follow-up."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "8f9bc8b1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import seaborn as sns\n",
|
||||||
|
"import matplotlib.pyplot as plt"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "1be11d54",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Load patient data"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "8dfc3020",
|
||||||
|
"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>patient-id</th>\n",
|
||||||
|
" <th>location-id</th>\n",
|
||||||
|
" <th>sex</th>\n",
|
||||||
|
" <th>age</th>\n",
|
||||||
|
" <th>smoke</th>\n",
|
||||||
|
" <th>bmi</th>\n",
|
||||||
|
" <th>waist</th>\n",
|
||||||
|
" <th>wth</th>\n",
|
||||||
|
" <th>htn</th>\n",
|
||||||
|
" <th>diab</th>\n",
|
||||||
|
" <th>hyperchol</th>\n",
|
||||||
|
" <th>famhist</th>\n",
|
||||||
|
" <th>hormo</th>\n",
|
||||||
|
" <th>p14</th>\n",
|
||||||
|
" <th>toevent</th>\n",
|
||||||
|
" <th>event</th>\n",
|
||||||
|
" <th>group</th>\n",
|
||||||
|
" <th>City</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>77</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>25.92</td>\n",
|
||||||
|
" <td>94</td>\n",
|
||||||
|
" <td>0.657343</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>5.538672</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>68</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>34.85</td>\n",
|
||||||
|
" <td>150</td>\n",
|
||||||
|
" <td>0.949367</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>10</td>\n",
|
||||||
|
" <td>3.063655</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>3</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>66</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>37.50</td>\n",
|
||||||
|
" <td>120</td>\n",
|
||||||
|
" <td>0.750000</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>6</td>\n",
|
||||||
|
" <td>5.590691</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>4</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>77</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>29.26</td>\n",
|
||||||
|
" <td>93</td>\n",
|
||||||
|
" <td>0.628378</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>6</td>\n",
|
||||||
|
" <td>5.456537</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>60</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>30.02</td>\n",
|
||||||
|
" <td>104</td>\n",
|
||||||
|
" <td>0.662420</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>2.746064</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>Control</td>\n",
|
||||||
|
" <td>Madrid</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>...</th>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>6240</th>\n",
|
||||||
|
" <td>1253</td>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>Male</td>\n",
|
||||||
|
" <td>79</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>25.28</td>\n",
|
||||||
|
" <td>105</td>\n",
|
||||||
|
" <td>0.640244</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>8</td>\n",
|
||||||
|
" <td>5.828884</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" <td>Malaga</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>6241</th>\n",
|
||||||
|
" <td>1254</td>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>Male</td>\n",
|
||||||
|
" <td>62</td>\n",
|
||||||
|
" <td>Former</td>\n",
|
||||||
|
" <td>27.10</td>\n",
|
||||||
|
" <td>104</td>\n",
|
||||||
|
" <td>0.594286</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>5.067762</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" <td>Malaga</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>6242</th>\n",
|
||||||
|
" <td>1255</td>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>Female</td>\n",
|
||||||
|
" <td>65</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>35.02</td>\n",
|
||||||
|
" <td>103</td>\n",
|
||||||
|
" <td>0.686667</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>10</td>\n",
|
||||||
|
" <td>1.993155</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + VOO</td>\n",
|
||||||
|
" <td>Malaga</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>6243</th>\n",
|
||||||
|
" <td>1256</td>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>Male</td>\n",
|
||||||
|
" <td>61</td>\n",
|
||||||
|
" <td>Never</td>\n",
|
||||||
|
" <td>28.42</td>\n",
|
||||||
|
" <td>94</td>\n",
|
||||||
|
" <td>0.576687</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>2.039699</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" <td>Malaga</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>6244</th>\n",
|
||||||
|
" <td>1257</td>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>Male</td>\n",
|
||||||
|
" <td>58</td>\n",
|
||||||
|
" <td>Former</td>\n",
|
||||||
|
" <td>24.43</td>\n",
|
||||||
|
" <td>93</td>\n",
|
||||||
|
" <td>0.547059</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>Yes</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>No</td>\n",
|
||||||
|
" <td>9</td>\n",
|
||||||
|
" <td>2.590007</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" <td>MedDiet + Nuts</td>\n",
|
||||||
|
" <td>Malaga</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"<p>6245 rows × 18 columns</p>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" patient-id location-id sex age smoke bmi waist wth \\\n",
|
||||||
|
"0 1 1 Female 77 Never 25.92 94 0.657343 \n",
|
||||||
|
"1 2 1 Female 68 Never 34.85 150 0.949367 \n",
|
||||||
|
"2 3 1 Female 66 Never 37.50 120 0.750000 \n",
|
||||||
|
"3 4 1 Female 77 Never 29.26 93 0.628378 \n",
|
||||||
|
"4 5 1 Female 60 Never 30.02 104 0.662420 \n",
|
||||||
|
"... ... ... ... ... ... ... ... ... \n",
|
||||||
|
"6240 1253 5 Male 79 Never 25.28 105 0.640244 \n",
|
||||||
|
"6241 1254 5 Male 62 Former 27.10 104 0.594286 \n",
|
||||||
|
"6242 1255 5 Female 65 Never 35.02 103 0.686667 \n",
|
||||||
|
"6243 1256 5 Male 61 Never 28.42 94 0.576687 \n",
|
||||||
|
"6244 1257 5 Male 58 Former 24.43 93 0.547059 \n",
|
||||||
|
"\n",
|
||||||
|
" htn diab hyperchol famhist hormo p14 toevent event group \\\n",
|
||||||
|
"0 Yes No Yes Yes No 9 5.538672 0 MedDiet + VOO \n",
|
||||||
|
"1 Yes No Yes Yes NaN 10 3.063655 0 MedDiet + Nuts \n",
|
||||||
|
"2 Yes Yes No No No 6 5.590691 0 MedDiet + Nuts \n",
|
||||||
|
"3 Yes Yes No No No 6 5.456537 0 MedDiet + VOO \n",
|
||||||
|
"4 Yes No Yes No No 9 2.746064 0 Control \n",
|
||||||
|
"... ... ... ... ... ... ... ... ... ... \n",
|
||||||
|
"6240 Yes No Yes No No 8 5.828884 0 MedDiet + VOO \n",
|
||||||
|
"6241 Yes No Yes Yes No 9 5.067762 0 MedDiet + Nuts \n",
|
||||||
|
"6242 Yes No Yes No No 10 1.993155 0 MedDiet + VOO \n",
|
||||||
|
"6243 Yes Yes No No No 9 2.039699 0 MedDiet + Nuts \n",
|
||||||
|
"6244 Yes Yes Yes No No 9 2.590007 0 MedDiet + Nuts \n",
|
||||||
|
"\n",
|
||||||
|
" City \n",
|
||||||
|
"0 Madrid \n",
|
||||||
|
"1 Madrid \n",
|
||||||
|
"2 Madrid \n",
|
||||||
|
"3 Madrid \n",
|
||||||
|
"4 Madrid \n",
|
||||||
|
"... ... \n",
|
||||||
|
"6240 Malaga \n",
|
||||||
|
"6241 Malaga \n",
|
||||||
|
"6242 Malaga \n",
|
||||||
|
"6243 Malaga \n",
|
||||||
|
"6244 Malaga \n",
|
||||||
|
"\n",
|
||||||
|
"[6245 rows x 18 columns]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df = pd.read_csv('processed_data_predimed.csv')\n",
|
||||||
|
"df['event'] = df['event'].map({'Yes': 1, 'No': 0})\n",
|
||||||
|
"df"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "db0d21f7-033e-48ca-9c90-81451af57003",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# calculate cumulative number of cases across time, independently for each group\n",
|
||||||
|
"\n",
|
||||||
|
"# your code here:\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "7ee10840-12ce-491b-b48a-5fb04df22919",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If you do it right, the following code will create a visualization as shown in the slides. Uncomment it"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "de6ee1a8",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#sns.lineplot(data=df.sort_values('toevent'), x='toevent', y='cumulative_event_count', hue='group')\n",
|
||||||
|
"#plt.ylabel('Cumulative events')\n",
|
||||||
|
"#plt.xlabel('Years of follow up (variable `toevent`)')\n",
|
||||||
|
"#sns.despine()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "b8f7a0b7-1ae0-470c-a153-59dc8a6caa28",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Optional exercise\n",
|
||||||
|
"\n",
|
||||||
|
"Redo the plot but with the cummulative *percentage* of cases. For that you need to divide the cummulative count by the total number of cases in each group. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "d63a56b1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# your code here:\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "ffd0e784-ab11-4073-a14a-038ef87c5464",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.11.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
1213
exercises/tabular_window_functions/window_functions_solution.ipynb
Normal file
1213
exercises/tabular_window_functions/window_functions_solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue