{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Impoting required packages\n",
"import pandas as pd\n",
"import numpy as np\n",
"import zipfile\n",
"\n",
"# setting default option\n",
"pd.set_option(\"mode.copy_on_write\", True)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Underlying Cause of Death, 2009.txt',\n",
" '__MACOSX/',\n",
" '__MACOSX/._Underlying Cause of Death, 2009.txt',\n",
" 'Underlying Cause of Death, 2008.txt',\n",
" 'Underlying Cause of Death, 2003.txt',\n",
" 'Underlying Cause of Death, 2014.txt',\n",
" 'Underlying Cause of Death, 2015.txt',\n",
" 'Underlying Cause of Death, 2005.txt',\n",
" 'Underlying Cause of Death, 2011.txt',\n",
" 'Underlying Cause of Death, 2010.txt',\n",
" 'Underlying Cause of Death, 2004.txt',\n",
" 'Underlying Cause of Death, 2012.txt',\n",
" 'Underlying Cause of Death, 2006.txt',\n",
" 'Underlying Cause of Death, 2007.txt',\n",
" 'Underlying Cause of Death, 2013.txt']"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# View the files present in the Zip file\n",
"z = zipfile.ZipFile(\"../.01_Data/01_Raw/raw_mortality.zip\")\n",
"z.namelist()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Underlying Cause of Death, 2003.txt',\n",
" 'Underlying Cause of Death, 2004.txt',\n",
" 'Underlying Cause of Death, 2005.txt',\n",
" 'Underlying Cause of Death, 2006.txt',\n",
" 'Underlying Cause of Death, 2007.txt',\n",
" 'Underlying Cause of Death, 2008.txt',\n",
" 'Underlying Cause of Death, 2009.txt',\n",
" 'Underlying Cause of Death, 2010.txt',\n",
" 'Underlying Cause of Death, 2011.txt',\n",
" 'Underlying Cause of Death, 2012.txt',\n",
" 'Underlying Cause of Death, 2013.txt',\n",
" 'Underlying Cause of Death, 2014.txt',\n",
" 'Underlying Cause of Death, 2015.txt']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating list of files which start with \"Underlying\" so as to ignore system files\n",
"file_list = sorted([f for f in z.namelist() if f.startswith(\"Underlying\")])\n",
"file_list"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Notes | \n",
" County | \n",
" County Code | \n",
" Year | \n",
" Year Code | \n",
" Drug/Alcohol Induced Cause | \n",
" Drug/Alcohol Induced Cause Code | \n",
" Deaths | \n",
"
\n",
" \n",
" \n",
" \n",
" 3308 | \n",
" NaN | \n",
" Union County, TN | \n",
" 47173.0 | \n",
" 2003.0 | \n",
" 2003.0 | \n",
" All other non-drug and non-alcohol causes | \n",
" O9 | \n",
" 165.0 | \n",
"
\n",
" \n",
" 2597 | \n",
" NaN | \n",
" Rutherford County, NC | \n",
" 37161.0 | \n",
" 2003.0 | \n",
" 2003.0 | \n",
" All other non-drug and non-alcohol causes | \n",
" O9 | \n",
" 731.0 | \n",
"
\n",
" \n",
" 717 | \n",
" NaN | \n",
" Glascock County, GA | \n",
" 13125.0 | \n",
" 2003.0 | \n",
" 2003.0 | \n",
" All other non-drug and non-alcohol causes | \n",
" O9 | \n",
" 40.0 | \n",
"
\n",
" \n",
" 2261 | \n",
" NaN | \n",
" Hillsborough County, NH | \n",
" 33011.0 | \n",
" 2003.0 | \n",
" 2003.0 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" D1 | \n",
" 23.0 | \n",
"
\n",
" \n",
" 3181 | \n",
" NaN | \n",
" McPherson County, SD | \n",
" 46089.0 | \n",
" 2003.0 | \n",
" 2003.0 | \n",
" All other non-drug and non-alcohol causes | \n",
" O9 | \n",
" 39.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Notes County County Code Year Year Code \\\n",
"3308 NaN Union County, TN 47173.0 2003.0 2003.0 \n",
"2597 NaN Rutherford County, NC 37161.0 2003.0 2003.0 \n",
"717 NaN Glascock County, GA 13125.0 2003.0 2003.0 \n",
"2261 NaN Hillsborough County, NH 33011.0 2003.0 2003.0 \n",
"3181 NaN McPherson County, SD 46089.0 2003.0 2003.0 \n",
"\n",
" Drug/Alcohol Induced Cause \\\n",
"3308 All other non-drug and non-alcohol causes \n",
"2597 All other non-drug and non-alcohol causes \n",
"717 All other non-drug and non-alcohol causes \n",
"2261 Drug poisonings (overdose) Unintentional (X40-... \n",
"3181 All other non-drug and non-alcohol causes \n",
"\n",
" Drug/Alcohol Induced Cause Code Deaths \n",
"3308 O9 165.0 \n",
"2597 O9 731.0 \n",
"717 O9 40.0 \n",
"2261 D1 23.0 \n",
"3181 O9 39.0 "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# read a single file to understand structure and cleaning rules required\n",
"test = pd.read_csv(z.open(file_list[0]), sep=\"\\t\")\n",
"test.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RangeIndex: 4102 entries, 0 to 4101\n",
"Data columns (total 8 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 Notes 15 non-null object \n",
" 1 County 4087 non-null object \n",
" 2 County Code 4087 non-null float64\n",
" 3 Year 4087 non-null float64\n",
" 4 Year Code 4087 non-null float64\n",
" 5 Drug/Alcohol Induced Cause 4087 non-null object \n",
" 6 Drug/Alcohol Induced Cause Code 4087 non-null object \n",
" 7 Deaths 4087 non-null float64\n",
"dtypes: float64(4), object(4)\n",
"memory usage: 256.5+ KB\n"
]
}
],
"source": [
"test.info()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Notes | \n",
" County | \n",
" County Code | \n",
" Year | \n",
" Year Code | \n",
" Drug/Alcohol Induced Cause | \n",
" Drug/Alcohol Induced Cause Code | \n",
" Deaths | \n",
"
\n",
" \n",
" \n",
" \n",
" 4087 | \n",
" --- | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4088 | \n",
" Dataset: Underlying Cause of Death, 1999-2017 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4089 | \n",
" Query Parameters: | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4090 | \n",
" Group By: County; Year; Drug/Alcohol Induced C... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4091 | \n",
" Show Totals: Disabled | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4092 | \n",
" Show Zero Values: Disabled | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4093 | \n",
" Show Suppressed: False | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4094 | \n",
" --- | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4095 | \n",
" Help: See http://wonder.cdc.gov/wonder/help/uc... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4096 | \n",
" --- | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4097 | \n",
" Suggested Citation: Centers for Disease Contro... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4098 | \n",
" 1999-2017 on CDC WONDER Online Database, relea... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4099 | \n",
" compiled from data provided by the 57 vital st... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4100 | \n",
" at http://wonder.cdc.gov/ucd-icd10.html on Oct... | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4101 | \n",
" --- | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Notes County County Code \\\n",
"4087 --- NaN NaN \n",
"4088 Dataset: Underlying Cause of Death, 1999-2017 NaN NaN \n",
"4089 Query Parameters: NaN NaN \n",
"4090 Group By: County; Year; Drug/Alcohol Induced C... NaN NaN \n",
"4091 Show Totals: Disabled NaN NaN \n",
"4092 Show Zero Values: Disabled NaN NaN \n",
"4093 Show Suppressed: False NaN NaN \n",
"4094 --- NaN NaN \n",
"4095 Help: See http://wonder.cdc.gov/wonder/help/uc... NaN NaN \n",
"4096 --- NaN NaN \n",
"4097 Suggested Citation: Centers for Disease Contro... NaN NaN \n",
"4098 1999-2017 on CDC WONDER Online Database, relea... NaN NaN \n",
"4099 compiled from data provided by the 57 vital st... NaN NaN \n",
"4100 at http://wonder.cdc.gov/ucd-icd10.html on Oct... NaN NaN \n",
"4101 --- NaN NaN \n",
"\n",
" Year Year Code Drug/Alcohol Induced Cause \\\n",
"4087 NaN NaN NaN \n",
"4088 NaN NaN NaN \n",
"4089 NaN NaN NaN \n",
"4090 NaN NaN NaN \n",
"4091 NaN NaN NaN \n",
"4092 NaN NaN NaN \n",
"4093 NaN NaN NaN \n",
"4094 NaN NaN NaN \n",
"4095 NaN NaN NaN \n",
"4096 NaN NaN NaN \n",
"4097 NaN NaN NaN \n",
"4098 NaN NaN NaN \n",
"4099 NaN NaN NaN \n",
"4100 NaN NaN NaN \n",
"4101 NaN NaN NaN \n",
"\n",
" Drug/Alcohol Induced Cause Code Deaths \n",
"4087 NaN NaN \n",
"4088 NaN NaN \n",
"4089 NaN NaN \n",
"4090 NaN NaN \n",
"4091 NaN NaN \n",
"4092 NaN NaN \n",
"4093 NaN NaN \n",
"4094 NaN NaN \n",
"4095 NaN NaN \n",
"4096 NaN NaN \n",
"4097 NaN NaN \n",
"4098 NaN NaN \n",
"4099 NaN NaN \n",
"4100 NaN NaN \n",
"4101 NaN NaN "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# viewing the rows which have non-null values in Notes column\n",
"test[test[\"Notes\"].notnull()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can clean notes in a similar was as we did for the other dataset"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# read data from all the files and append to list\n",
"df_list = []\n",
"for file in file_list:\n",
" # read individual files\n",
" df_temp = pd.read_csv(z.open(file), sep=\"\\t\")\n",
"\n",
" # drop the notes columns and remove rows with null values in State column\n",
" df_temp.drop(columns=[\"Notes\"], inplace=True)\n",
" df_temp.dropna(subset=[\"County\"], inplace=True)\n",
"\n",
" # add the cleaned temp Df to the main list\n",
" df_list.append(df_temp)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" County | \n",
" County Code | \n",
" Year | \n",
" Year Code | \n",
" Drug/Alcohol Induced Cause | \n",
" Drug/Alcohol Induced Cause Code | \n",
" Deaths | \n",
"
\n",
" \n",
" \n",
" \n",
" 32856 | \n",
" Sequoyah County, OK | \n",
" 40135.0 | \n",
" 2010.0 | \n",
" 2010.0 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" D1 | \n",
" 11.0 | \n",
"
\n",
" \n",
" 11096 | \n",
" Stark County, OH | \n",
" 39151.0 | \n",
" 2005.0 | \n",
" 2005.0 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" D1 | \n",
" 16.0 | \n",
"
\n",
" \n",
" 48071 | \n",
" Butte County, CA | \n",
" 6007.0 | \n",
" 2014.0 | \n",
" 2014.0 | \n",
" All other alcohol-induced causes | \n",
" A9 | \n",
" 42.0 | \n",
"
\n",
" \n",
" 2822 | \n",
" Garfield County, OK | \n",
" 40047.0 | \n",
" 2003.0 | \n",
" 2003.0 | \n",
" All other non-drug and non-alcohol causes | \n",
" O9 | \n",
" 676.0 | \n",
"
\n",
" \n",
" 35841 | \n",
" Terrebonne Parish, LA | \n",
" 22109.0 | \n",
" 2011.0 | \n",
" 2011.0 | \n",
" All other non-drug and non-alcohol causes | \n",
" O9 | \n",
" 952.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" County County Code Year Year Code \\\n",
"32856 Sequoyah County, OK 40135.0 2010.0 2010.0 \n",
"11096 Stark County, OH 39151.0 2005.0 2005.0 \n",
"48071 Butte County, CA 6007.0 2014.0 2014.0 \n",
"2822 Garfield County, OK 40047.0 2003.0 2003.0 \n",
"35841 Terrebonne Parish, LA 22109.0 2011.0 2011.0 \n",
"\n",
" Drug/Alcohol Induced Cause \\\n",
"32856 Drug poisonings (overdose) Unintentional (X40-... \n",
"11096 Drug poisonings (overdose) Unintentional (X40-... \n",
"48071 All other alcohol-induced causes \n",
"2822 All other non-drug and non-alcohol causes \n",
"35841 All other non-drug and non-alcohol causes \n",
"\n",
" Drug/Alcohol Induced Cause Code Deaths \n",
"32856 D1 11.0 \n",
"11096 D1 16.0 \n",
"48071 A9 42.0 \n",
"2822 O9 676.0 \n",
"35841 O9 952.0 "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create the dataframe\n",
"df = pd.concat(df_list, ignore_index=True)\n",
"df.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"County 0\n",
"County Code 0\n",
"Year 0\n",
"Year Code 0\n",
"Drug/Alcohol Induced Cause 0\n",
"Drug/Alcohol Induced Cause Code 0\n",
"Deaths 0\n",
"dtype: int64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check for null values\n",
"df.isnull().sum()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"RangeIndex: 57241 entries, 0 to 57240\n",
"Data columns (total 7 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 County 57241 non-null object \n",
" 1 County Code 57241 non-null float64\n",
" 2 Year 57241 non-null float64\n",
" 3 Year Code 57241 non-null float64\n",
" 4 Drug/Alcohol Induced Cause 57241 non-null object \n",
" 5 Drug/Alcohol Induced Cause Code 57241 non-null object \n",
" 6 Deaths 57241 non-null object \n",
"dtypes: float64(3), object(4)\n",
"memory usage: 3.1+ MB\n"
]
}
],
"source": [
"df.info()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Cleaning the data\n",
"df2 = df.copy()\n",
"\n",
"# Pad county code with 0 for consistency with other data sets\n",
"df2[\"County Code\"] = df2[\"County Code\"].astype(int).astype(str).str.zfill(5)\n",
"\n",
"# Convert Year to Int\n",
"df2[\"Year\"] = df2[\"Year\"].astype(int)\n",
"\n",
"# Convert Deaths to Int\n",
"df2[\"Deaths\"] = df2[\"Deaths\"].replace(\"Missing\", np.nan)\n",
"df2[\"Deaths\"] = (\n",
" df2[\"Deaths\"].astype(float).astype(\"Int64\")\n",
") # making it as int64 so that we retain null values"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['All other non-drug and non-alcohol causes',\n",
" 'Drug poisonings (overdose) Unintentional (X40-X44)',\n",
" 'All other alcohol-induced causes',\n",
" 'All other drug-induced causes',\n",
" 'Drug poisonings (overdose) Suicide (X60-X64)',\n",
" 'Drug poisonings (overdose) Undetermined (Y10-Y14)',\n",
" 'Alcohol poisonings (overdose) (X45, X65, Y15)',\n",
" 'Drug poisonings (overdose) Homicide (X85)'], dtype=object)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check the causes of death present\n",
"df2[\"Drug/Alcohol Induced Cause\"].unique()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Filter the data to only include drug related deaths\n",
"required_causes = [\n",
" \"Drug poisonings (overdose) Unintentional (X40-X44)\",\n",
" \"All other drug-induced causes\",\n",
" \"Drug poisonings (overdose) Homicide (X85)\",\n",
" \"Drug poisonings (overdose) Suicide (X60-X64)\",\n",
" \"Drug poisonings (overdose) Undetermined (Y10-Y14)\",\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"df3 = df2[df2[\"Drug/Alcohol Induced Cause\"].isin(required_causes)]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# remove extra columns\n",
"df3.drop(columns=[\"Year Code\", \"Drug/Alcohol Induced Cause Code\"], inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" County | \n",
" County Code | \n",
" Year | \n",
" Drug/Alcohol Induced Cause | \n",
" Deaths | \n",
"
\n",
" \n",
" \n",
" \n",
" 30939 | \n",
" Wabash County, IN | \n",
" 18169 | \n",
" 2010 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 10 | \n",
"
\n",
" \n",
" 36770 | \n",
" Cayuga County, NY | \n",
" 36011 | \n",
" 2011 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 10 | \n",
"
\n",
" \n",
" 2317 | \n",
" Passaic County, NJ | \n",
" 34031 | \n",
" 2003 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 28 | \n",
"
\n",
" \n",
" 9869 | \n",
" Frederick County, MD | \n",
" 24021 | \n",
" 2005 | \n",
" Drug poisonings (overdose) Undetermined (Y10-Y14) | \n",
" 13 | \n",
"
\n",
" \n",
" 22426 | \n",
" Boyd County, KY | \n",
" 21019 | \n",
" 2008 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 24 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" County County Code Year \\\n",
"30939 Wabash County, IN 18169 2010 \n",
"36770 Cayuga County, NY 36011 2011 \n",
"2317 Passaic County, NJ 34031 2003 \n",
"9869 Frederick County, MD 24021 2005 \n",
"22426 Boyd County, KY 21019 2008 \n",
"\n",
" Drug/Alcohol Induced Cause Deaths \n",
"30939 Drug poisonings (overdose) Unintentional (X40-... 10 \n",
"36770 Drug poisonings (overdose) Unintentional (X40-... 10 \n",
"2317 Drug poisonings (overdose) Unintentional (X40-... 28 \n",
"9869 Drug poisonings (overdose) Undetermined (Y10-Y14) 13 \n",
"22426 Drug poisonings (overdose) Unintentional (X40-... 24 "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# renaming columns\n",
"df3.rename(\n",
" columns={\"Drug/Alcohol Induced Cause\": \"Cause\", \"County Code\": \"County_Code\"},\n",
" inplace=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# use fips file to generate proper county name and state\n",
"fips = pd.read_csv(\"../.01_Data/01_Raw/county_fips.csv\")\n",
"fips[\"countyfips\"] = fips[\"countyfips\"].astype(str).str.zfill(5)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" County | \n",
" County_Code | \n",
" Year | \n",
" Cause | \n",
" Deaths | \n",
" BUYER_COUNTY | \n",
" BUYER_STATE | \n",
" countyfips | \n",
" _merge | \n",
"
\n",
" \n",
" \n",
" \n",
" 1799 | \n",
" Hidalgo County, TX | \n",
" 48215 | \n",
" 2005 | \n",
" All other drug-induced causes | \n",
" 10 | \n",
" HIDALGO | \n",
" TX | \n",
" 48215 | \n",
" both | \n",
"
\n",
" \n",
" 9215 | \n",
" Greenville County, SC | \n",
" 45045 | \n",
" 2014 | \n",
" Drug poisonings (overdose) Suicide (X60-X64) | \n",
" 10 | \n",
" GREENVILLE | \n",
" SC | \n",
" 45045 | \n",
" both | \n",
"
\n",
" \n",
" 2843 | \n",
" St. Clair County, IL | \n",
" 17163 | \n",
" 2007 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 15 | \n",
" SAINT CLAIR | \n",
" IL | \n",
" 17163 | \n",
" both | \n",
"
\n",
" \n",
" 1063 | \n",
" Northampton County, PA | \n",
" 42095 | \n",
" 2004 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 16 | \n",
" NORTHAMPTON | \n",
" PA | \n",
" 42095 | \n",
" both | \n",
"
\n",
" \n",
" 4368 | \n",
" Newton County, GA | \n",
" 13217 | \n",
" 2009 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 13 | \n",
" NEWTON | \n",
" GA | \n",
" 13217 | \n",
" both | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" County County_Code Year \\\n",
"1799 Hidalgo County, TX 48215 2005 \n",
"9215 Greenville County, SC 45045 2014 \n",
"2843 St. Clair County, IL 17163 2007 \n",
"1063 Northampton County, PA 42095 2004 \n",
"4368 Newton County, GA 13217 2009 \n",
"\n",
" Cause Deaths BUYER_COUNTY \\\n",
"1799 All other drug-induced causes 10 HIDALGO \n",
"9215 Drug poisonings (overdose) Suicide (X60-X64) 10 GREENVILLE \n",
"2843 Drug poisonings (overdose) Unintentional (X40-... 15 SAINT CLAIR \n",
"1063 Drug poisonings (overdose) Unintentional (X40-... 16 NORTHAMPTON \n",
"4368 Drug poisonings (overdose) Unintentional (X40-... 13 NEWTON \n",
"\n",
" BUYER_STATE countyfips _merge \n",
"1799 TX 48215 both \n",
"9215 SC 45045 both \n",
"2843 IL 17163 both \n",
"1063 PA 42095 both \n",
"4368 GA 13217 both "
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# merge with fips\n",
"# performing left join to get the county names\n",
"df4 = pd.merge(\n",
" df3,\n",
" fips,\n",
" how=\"left\",\n",
" left_on=\"County_Code\",\n",
" right_on=\"countyfips\",\n",
" validate=\"m:1\",\n",
" indicator=True,\n",
")\n",
"df4.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"_merge\n",
"both 10432\n",
"left_only 0\n",
"right_only 0\n",
"Name: count, dtype: int64"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Validate if merge went well\n",
"df4[\"_merge\"].value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"# select required colums\n",
"df5 = df4[[\"BUYER_STATE\", \"BUYER_COUNTY\", \"County_Code\", \"Year\", \"Cause\", \"Deaths\"]]\n",
"\n",
"# rename columns\n",
"df5 = df5.rename(columns={\"BUYER_COUNTY\": \"County\", \"BUYER_STATE\": \"State_Code\"})"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" State_Code | \n",
" County | \n",
" County_Code | \n",
" Year | \n",
" Cause | \n",
" Deaths | \n",
"
\n",
" \n",
" \n",
" \n",
" 2201 | \n",
" MI | \n",
" BERRIEN | \n",
" 26021 | \n",
" 2006 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 21 | \n",
"
\n",
" \n",
" 3238 | \n",
" TN | \n",
" BRADLEY | \n",
" 47011 | \n",
" 2007 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 23 | \n",
"
\n",
" \n",
" 8909 | \n",
" MS | \n",
" MADISON | \n",
" 28089 | \n",
" 2014 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 12 | \n",
"
\n",
" \n",
" 7578 | \n",
" AZ | \n",
" MARICOPA | \n",
" 04013 | \n",
" 2013 | \n",
" Drug poisonings (overdose) Undetermined (Y10-Y14) | \n",
" 89 | \n",
"
\n",
" \n",
" 5624 | \n",
" SC | \n",
" OCONEE | \n",
" 45073 | \n",
" 2010 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 16 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" State_Code County County_Code Year \\\n",
"2201 MI BERRIEN 26021 2006 \n",
"3238 TN BRADLEY 47011 2007 \n",
"8909 MS MADISON 28089 2014 \n",
"7578 AZ MARICOPA 04013 2013 \n",
"5624 SC OCONEE 45073 2010 \n",
"\n",
" Cause Deaths \n",
"2201 Drug poisonings (overdose) Unintentional (X40-... 21 \n",
"3238 Drug poisonings (overdose) Unintentional (X40-... 23 \n",
"8909 Drug poisonings (overdose) Unintentional (X40-... 12 \n",
"7578 Drug poisonings (overdose) Undetermined (Y10-Y14) 89 \n",
"5624 Drug poisonings (overdose) Unintentional (X40-... 16 "
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df5.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" abbrev | \n",
" code | \n",
"
\n",
" \n",
" \n",
" \n",
" 13 | \n",
" Illinois | \n",
" Ill. | \n",
" IL | \n",
"
\n",
" \n",
" 40 | \n",
" South Carolina | \n",
" S.C. | \n",
" SC | \n",
"
\n",
" \n",
" 10 | \n",
" Georgia | \n",
" Ga. | \n",
" GA | \n",
"
\n",
" \n",
" 34 | \n",
" North Dakota | \n",
" N.D. | \n",
" ND | \n",
"
\n",
" \n",
" 38 | \n",
" Pennsylvania | \n",
" Pa. | \n",
" PA | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state abbrev code\n",
"13 Illinois Ill. IL\n",
"40 South Carolina S.C. SC\n",
"10 Georgia Ga. GA\n",
"34 North Dakota N.D. ND\n",
"38 Pennsylvania Pa. PA"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Add state names to maitain consistency with population data\n",
"abbreviations = pd.read_csv(\"../.01_Data/01_Raw/state_abbreviations.csv\")\n",
"abbreviations.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# rename colums to match with the main dataframe\n",
"abbreviations = abbreviations.rename(\n",
" columns={\n",
" \"state\": \"State\",\n",
" \"code\": \"State_Code\",\n",
" }\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" State_Code | \n",
" County | \n",
" County_Code | \n",
" Year | \n",
" Cause | \n",
" Deaths | \n",
" State | \n",
" _merge | \n",
"
\n",
" \n",
" \n",
" \n",
" 6372 | \n",
" OH | \n",
" DELAWARE | \n",
" 39041 | \n",
" 2011 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 11 | \n",
" Ohio | \n",
" both | \n",
"
\n",
" \n",
" 5339 | \n",
" MI | \n",
" WASHTENAW | \n",
" 26161 | \n",
" 2010 | \n",
" Drug poisonings (overdose) Undetermined (Y10-Y14) | \n",
" 19 | \n",
" Michigan | \n",
" both | \n",
"
\n",
" \n",
" 8433 | \n",
" WV | \n",
" WAYNE | \n",
" 54099 | \n",
" 2013 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 17 | \n",
" West Virginia | \n",
" both | \n",
"
\n",
" \n",
" 4737 | \n",
" OR | \n",
" CLACKAMAS | \n",
" 41005 | \n",
" 2009 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 32 | \n",
" Oregon | \n",
" both | \n",
"
\n",
" \n",
" 93 | \n",
" CO | \n",
" LARIMER | \n",
" 08069 | \n",
" 2003 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 13 | \n",
" Colorado | \n",
" both | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" State_Code County County_Code Year \\\n",
"6372 OH DELAWARE 39041 2011 \n",
"5339 MI WASHTENAW 26161 2010 \n",
"8433 WV WAYNE 54099 2013 \n",
"4737 OR CLACKAMAS 41005 2009 \n",
"93 CO LARIMER 08069 2003 \n",
"\n",
" Cause Deaths \\\n",
"6372 Drug poisonings (overdose) Unintentional (X40-... 11 \n",
"5339 Drug poisonings (overdose) Undetermined (Y10-Y14) 19 \n",
"8433 Drug poisonings (overdose) Unintentional (X40-... 17 \n",
"4737 Drug poisonings (overdose) Unintentional (X40-... 32 \n",
"93 Drug poisonings (overdose) Unintentional (X40-... 13 \n",
"\n",
" State _merge \n",
"6372 Ohio both \n",
"5339 Michigan both \n",
"8433 West Virginia both \n",
"4737 Oregon both \n",
"93 Colorado both "
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Merge\n",
"df6 = pd.merge(\n",
" df5,\n",
" abbreviations[[\"State\", \"State_Code\"]],\n",
" how=\"left\",\n",
" on=\"State_Code\",\n",
" validate=\"m:1\",\n",
" indicator=True,\n",
")\n",
"df6.sample(5)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"_merge\n",
"both 10432\n",
"left_only 0\n",
"right_only 0\n",
"Name: count, dtype: int64"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Validate if merge went well\n",
"df6[\"_merge\"].value_counts()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In script file we dont need the merge indicator column, so it will not be used there"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" State | \n",
" State_Code | \n",
" County | \n",
" County_Code | \n",
" Year | \n",
" Cause | \n",
" Deaths | \n",
"
\n",
" \n",
" \n",
" \n",
" 2398 | \n",
" Ohio | \n",
" OH | \n",
" TRUMBULL | \n",
" 39155 | \n",
" 2006 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 30 | \n",
"
\n",
" \n",
" 5703 | \n",
" Utah | \n",
" UT | \n",
" DAVIS | \n",
" 49011 | \n",
" 2010 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 22 | \n",
"
\n",
" \n",
" 10208 | \n",
" South Carolina | \n",
" SC | \n",
" AIKEN | \n",
" 45003 | \n",
" 2015 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 30 | \n",
"
\n",
" \n",
" 242 | \n",
" Maryland | \n",
" MD | \n",
" PRINCE GEORGES | \n",
" 24033 | \n",
" 2003 | \n",
" Drug poisonings (overdose) Unintentional (X40-... | \n",
" 14 | \n",
"
\n",
" \n",
" 1250 | \n",
" California | \n",
" CA | \n",
" MARIN | \n",
" 06041 | \n",
" 2005 | \n",
" Drug poisonings (overdose) Undetermined (Y10-Y14) | \n",
" 16 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" State State_Code County County_Code Year \\\n",
"2398 Ohio OH TRUMBULL 39155 2006 \n",
"5703 Utah UT DAVIS 49011 2010 \n",
"10208 South Carolina SC AIKEN 45003 2015 \n",
"242 Maryland MD PRINCE GEORGES 24033 2003 \n",
"1250 California CA MARIN 06041 2005 \n",
"\n",
" Cause Deaths \n",
"2398 Drug poisonings (overdose) Unintentional (X40-... 30 \n",
"5703 Drug poisonings (overdose) Unintentional (X40-... 22 \n",
"10208 Drug poisonings (overdose) Unintentional (X40-... 30 \n",
"242 Drug poisonings (overdose) Unintentional (X40-... 14 \n",
"1250 Drug poisonings (overdose) Undetermined (Y10-Y14) 16 "
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# reorder columns to match population data\n",
"df6 = df6[[\"State\", \"State_Code\", \"County\", \"County_Code\", \"Year\", \"Cause\", \"Deaths\"]]\n",
"df6.sample(5)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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": 2
}