Create notebook for the second application of tidying
This commit is contained in:
parent
8e3449553d
commit
1a210cc799
1 changed files with 730 additions and 0 deletions
730
2_multiple_variables_stored_in_one_column.ipynb
Normal file
730
2_multiple_variables_stored_in_one_column.ipynb
Normal file
|
|
@ -0,0 +1,730 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Multiple Variables stored in one Column\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook shows how multiple variables stored in the same column can be isolated."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## \"Housekeeping\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"2018-08-26 11:50:39 CEST\n",
|
||||||
|
"\n",
|
||||||
|
"CPython 3.6.5\n",
|
||||||
|
"IPython 6.5.0\n",
|
||||||
|
"\n",
|
||||||
|
"numpy 1.15.1\n",
|
||||||
|
"pandas 0.23.4\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"% load_ext watermark\n",
|
||||||
|
"% watermark -d -t -v -z -p numpy,pandas"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Example: Tuberculosis"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Load the Data\n",
|
||||||
|
"\n",
|
||||||
|
"Select the same columns as in the paper and name them accordingly."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"columns = ['iso2', 'year',\n",
|
||||||
|
" 'new_sp_m014', 'new_sp_m1524', 'new_sp_m2534', 'new_sp_m3544',\n",
|
||||||
|
" 'new_sp_m4554', 'new_sp_m5564', 'new_sp_m65', 'new_sp_mu',\n",
|
||||||
|
" 'new_sp_f014', 'new_sp_f1524', 'new_sp_f2534', 'new_sp_f3544',\n",
|
||||||
|
" 'new_sp_f4554', 'new_sp_f5564', 'new_sp_f65', 'new_sp_fu']\n",
|
||||||
|
"tb = pd.read_csv('data/tb.csv', usecols=columns)\n",
|
||||||
|
"\n",
|
||||||
|
"rename = {c: c[7:] for c in columns if c.startswith('new_sp_')}\n",
|
||||||
|
"rename = {'iso2': 'country', **rename}\n",
|
||||||
|
"tb = tb.rename(columns=rename)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Messy Data\n",
|
||||||
|
"\n",
|
||||||
|
"The data are assumed to be provided as below. Except for the *country* and *year* columns, the remaining columns are actually joint realizations of two variables **sex** and **age**."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"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>m014</th>\n",
|
||||||
|
" <th>m1524</th>\n",
|
||||||
|
" <th>m2534</th>\n",
|
||||||
|
" <th>m3544</th>\n",
|
||||||
|
" <th>m4554</th>\n",
|
||||||
|
" <th>m5564</th>\n",
|
||||||
|
" <th>m65</th>\n",
|
||||||
|
" <th>mu</th>\n",
|
||||||
|
" <th>f014</th>\n",
|
||||||
|
" <th>f1524</th>\n",
|
||||||
|
" <th>f2534</th>\n",
|
||||||
|
" <th>f3544</th>\n",
|
||||||
|
" <th>f4554</th>\n",
|
||||||
|
" <th>f5564</th>\n",
|
||||||
|
" <th>f65</th>\n",
|
||||||
|
" <th>fu</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>10</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.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",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>36</th>\n",
|
||||||
|
" <td>AE</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>4.0</td>\n",
|
||||||
|
" <td>4.0</td>\n",
|
||||||
|
" <td>6.0</td>\n",
|
||||||
|
" <td>5.0</td>\n",
|
||||||
|
" <td>12.0</td>\n",
|
||||||
|
" <td>10.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>3.0</td>\n",
|
||||||
|
" <td>16.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>3.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>4.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>60</th>\n",
|
||||||
|
" <td>AF</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>52.0</td>\n",
|
||||||
|
" <td>228.0</td>\n",
|
||||||
|
" <td>183.0</td>\n",
|
||||||
|
" <td>149.0</td>\n",
|
||||||
|
" <td>129.0</td>\n",
|
||||||
|
" <td>94.0</td>\n",
|
||||||
|
" <td>80.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>93.0</td>\n",
|
||||||
|
" <td>414.0</td>\n",
|
||||||
|
" <td>565.0</td>\n",
|
||||||
|
" <td>339.0</td>\n",
|
||||||
|
" <td>205.0</td>\n",
|
||||||
|
" <td>99.0</td>\n",
|
||||||
|
" <td>36.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>87</th>\n",
|
||||||
|
" <td>AG</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>136</th>\n",
|
||||||
|
" <td>AL</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>19.0</td>\n",
|
||||||
|
" <td>21.0</td>\n",
|
||||||
|
" <td>14.0</td>\n",
|
||||||
|
" <td>24.0</td>\n",
|
||||||
|
" <td>19.0</td>\n",
|
||||||
|
" <td>16.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>3.0</td>\n",
|
||||||
|
" <td>11.0</td>\n",
|
||||||
|
" <td>10.0</td>\n",
|
||||||
|
" <td>8.0</td>\n",
|
||||||
|
" <td>8.0</td>\n",
|
||||||
|
" <td>5.0</td>\n",
|
||||||
|
" <td>11.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>165</th>\n",
|
||||||
|
" <td>AM</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>152.0</td>\n",
|
||||||
|
" <td>130.0</td>\n",
|
||||||
|
" <td>131.0</td>\n",
|
||||||
|
" <td>63.0</td>\n",
|
||||||
|
" <td>26.0</td>\n",
|
||||||
|
" <td>21.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>24.0</td>\n",
|
||||||
|
" <td>27.0</td>\n",
|
||||||
|
" <td>24.0</td>\n",
|
||||||
|
" <td>8.0</td>\n",
|
||||||
|
" <td>8.0</td>\n",
|
||||||
|
" <td>4.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>178</th>\n",
|
||||||
|
" <td>AN</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>207</th>\n",
|
||||||
|
" <td>AO</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>NaN</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",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>236</th>\n",
|
||||||
|
" <td>AR</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>97.0</td>\n",
|
||||||
|
" <td>278.0</td>\n",
|
||||||
|
" <td>594.0</td>\n",
|
||||||
|
" <td>402.0</td>\n",
|
||||||
|
" <td>419.0</td>\n",
|
||||||
|
" <td>368.0</td>\n",
|
||||||
|
" <td>330.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>121.0</td>\n",
|
||||||
|
" <td>544.0</td>\n",
|
||||||
|
" <td>479.0</td>\n",
|
||||||
|
" <td>262.0</td>\n",
|
||||||
|
" <td>230.0</td>\n",
|
||||||
|
" <td>179.0</td>\n",
|
||||||
|
" <td>216.0</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>265</th>\n",
|
||||||
|
" <td>AS</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>NaN</td>\n",
|
||||||
|
" <td>1.0</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>1.0</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 m014 m1524 m2534 m3544 m4554 m5564 m65 mu \\\n",
|
||||||
|
"10 AD 2000 0.0 0.0 1.0 0.0 0.0 0.0 0.0 NaN \n",
|
||||||
|
"36 AE 2000 2.0 4.0 4.0 6.0 5.0 12.0 10.0 NaN \n",
|
||||||
|
"60 AF 2000 52.0 228.0 183.0 149.0 129.0 94.0 80.0 NaN \n",
|
||||||
|
"87 AG 2000 0.0 0.0 0.0 0.0 0.0 0.0 1.0 NaN \n",
|
||||||
|
"136 AL 2000 2.0 19.0 21.0 14.0 24.0 19.0 16.0 NaN \n",
|
||||||
|
"165 AM 2000 2.0 152.0 130.0 131.0 63.0 26.0 21.0 NaN \n",
|
||||||
|
"178 AN 2000 0.0 0.0 1.0 2.0 0.0 0.0 0.0 NaN \n",
|
||||||
|
"207 AO 2000 186.0 999.0 1003.0 912.0 482.0 312.0 194.0 NaN \n",
|
||||||
|
"236 AR 2000 97.0 278.0 594.0 402.0 419.0 368.0 330.0 NaN \n",
|
||||||
|
"265 AS 2000 NaN NaN NaN NaN 1.0 1.0 NaN NaN \n",
|
||||||
|
"\n",
|
||||||
|
" f014 f1524 f2534 f3544 f4554 f5564 f65 fu \n",
|
||||||
|
"10 NaN NaN NaN NaN NaN NaN NaN NaN \n",
|
||||||
|
"36 3.0 16.0 1.0 3.0 0.0 0.0 4.0 NaN \n",
|
||||||
|
"60 93.0 414.0 565.0 339.0 205.0 99.0 36.0 NaN \n",
|
||||||
|
"87 1.0 1.0 1.0 0.0 0.0 0.0 0.0 NaN \n",
|
||||||
|
"136 3.0 11.0 10.0 8.0 8.0 5.0 11.0 NaN \n",
|
||||||
|
"165 1.0 24.0 27.0 24.0 8.0 8.0 4.0 NaN \n",
|
||||||
|
"178 0.0 0.0 1.0 0.0 0.0 1.0 0.0 NaN \n",
|
||||||
|
"207 247.0 1142.0 1091.0 844.0 417.0 200.0 120.0 NaN \n",
|
||||||
|
"236 121.0 544.0 479.0 262.0 230.0 179.0 216.0 NaN \n",
|
||||||
|
"265 NaN NaN NaN NaN 1.0 NaN NaN NaN "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tb[(tb['year'] == 2000)].head(10)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Molten Data\n",
|
||||||
|
"\n",
|
||||||
|
"As in the previous notebook the [*pd.melt*](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.melt.html) function can be used to un-pivot the columns. As before, pandas keeps rows for columns with missing data that are then discarded (then, without any more missing values, the column's data type is casted as integer). Furthermore, the resulting *molten* dataset is sorted as in the paper."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"molten_tb = pd.melt(tb, id_vars=['country', 'year'], var_name='column', value_name='cases')\n",
|
||||||
|
"molten_tb = molten_tb[molten_tb['cases'].notnull()]\n",
|
||||||
|
"molten_tb['cases'] = molten_tb['cases'].astype(int)\n",
|
||||||
|
"molten_tb = molten_tb.sort_values(['country', 'year', 'column']).reset_index(drop=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"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>column</th>\n",
|
||||||
|
" <th>cases</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>49</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m014</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>50</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m1524</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>51</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m2534</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>52</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m3544</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>53</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m4554</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>54</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m5564</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>55</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m65</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>165</th>\n",
|
||||||
|
" <td>AE</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>f014</td>\n",
|
||||||
|
" <td>3</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>166</th>\n",
|
||||||
|
" <td>AE</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>f1524</td>\n",
|
||||||
|
" <td>16</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>167</th>\n",
|
||||||
|
" <td>AE</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>f2534</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" country year column cases\n",
|
||||||
|
"49 AD 2000 m014 0\n",
|
||||||
|
"50 AD 2000 m1524 0\n",
|
||||||
|
"51 AD 2000 m2534 1\n",
|
||||||
|
"52 AD 2000 m3544 0\n",
|
||||||
|
"53 AD 2000 m4554 0\n",
|
||||||
|
"54 AD 2000 m5564 0\n",
|
||||||
|
"55 AD 2000 m65 0\n",
|
||||||
|
"165 AE 2000 f014 3\n",
|
||||||
|
"166 AE 2000 f1524 16\n",
|
||||||
|
"167 AE 2000 f2534 1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"molten_tb[(molten_tb['year'] == 2000)].head(10)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Tidy Data\n",
|
||||||
|
"\n",
|
||||||
|
"Using the [*pd.Series.str.extract*](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.extract.html) method the two variables are isolated. The age labels are renamed as in the paper."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"tidy_tb = molten_tb[['country', 'year', 'cases']]\n",
|
||||||
|
"tidy_tb[['sex', 'age']] = molten_tb['column'].str.extract(r'(f|m)(.*)')\n",
|
||||||
|
"tidy_tb['age'] = tidy_tb['age'].map({\n",
|
||||||
|
" '014': '0-14', '1524': '15-24', '2534': '25-34',\n",
|
||||||
|
" '3544': '35-44', '4554': '45-54', '5564': '55-64',\n",
|
||||||
|
" '65': '65+', 'u': 'unknown'\n",
|
||||||
|
"})\n",
|
||||||
|
"tidy_tb = tidy_tb[['country', 'year', 'sex', 'age', 'cases']]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"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>sex</th>\n",
|
||||||
|
" <th>age</th>\n",
|
||||||
|
" <th>cases</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>49</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m</td>\n",
|
||||||
|
" <td>0-14</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>50</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m</td>\n",
|
||||||
|
" <td>15-24</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>51</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m</td>\n",
|
||||||
|
" <td>25-34</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>52</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m</td>\n",
|
||||||
|
" <td>35-44</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>53</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m</td>\n",
|
||||||
|
" <td>45-54</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>54</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m</td>\n",
|
||||||
|
" <td>55-64</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>55</th>\n",
|
||||||
|
" <td>AD</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>m</td>\n",
|
||||||
|
" <td>65+</td>\n",
|
||||||
|
" <td>0</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>165</th>\n",
|
||||||
|
" <td>AE</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>f</td>\n",
|
||||||
|
" <td>0-14</td>\n",
|
||||||
|
" <td>3</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>166</th>\n",
|
||||||
|
" <td>AE</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>f</td>\n",
|
||||||
|
" <td>15-24</td>\n",
|
||||||
|
" <td>16</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>167</th>\n",
|
||||||
|
" <td>AE</td>\n",
|
||||||
|
" <td>2000</td>\n",
|
||||||
|
" <td>f</td>\n",
|
||||||
|
" <td>25-34</td>\n",
|
||||||
|
" <td>1</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"</div>"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
" country year sex age cases\n",
|
||||||
|
"49 AD 2000 m 0-14 0\n",
|
||||||
|
"50 AD 2000 m 15-24 0\n",
|
||||||
|
"51 AD 2000 m 25-34 1\n",
|
||||||
|
"52 AD 2000 m 35-44 0\n",
|
||||||
|
"53 AD 2000 m 45-54 0\n",
|
||||||
|
"54 AD 2000 m 55-64 0\n",
|
||||||
|
"55 AD 2000 m 65+ 0\n",
|
||||||
|
"165 AE 2000 f 0-14 3\n",
|
||||||
|
"166 AE 2000 f 15-24 16\n",
|
||||||
|
"167 AE 2000 f 25-34 1"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"tidy_tb[(tidy_tb['year'] == 2000)].head(10)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue