{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NotesCountyCounty CodeYearYear CodeDrug/Alcohol Induced CauseDrug/Alcohol Induced Cause CodeDeaths
3308NaNUnion County, TN47173.02003.02003.0All other non-drug and non-alcohol causesO9165.0
2597NaNRutherford County, NC37161.02003.02003.0All other non-drug and non-alcohol causesO9731.0
717NaNGlascock County, GA13125.02003.02003.0All other non-drug and non-alcohol causesO940.0
2261NaNHillsborough County, NH33011.02003.02003.0Drug poisonings (overdose) Unintentional (X40-...D123.0
3181NaNMcPherson County, SD46089.02003.02003.0All other non-drug and non-alcohol causesO939.0
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
NotesCountyCounty CodeYearYear CodeDrug/Alcohol Induced CauseDrug/Alcohol Induced Cause CodeDeaths
4087---NaNNaNNaNNaNNaNNaNNaN
4088Dataset: Underlying Cause of Death, 1999-2017NaNNaNNaNNaNNaNNaNNaN
4089Query Parameters:NaNNaNNaNNaNNaNNaNNaN
4090Group By: County; Year; Drug/Alcohol Induced C...NaNNaNNaNNaNNaNNaNNaN
4091Show Totals: DisabledNaNNaNNaNNaNNaNNaNNaN
4092Show Zero Values: DisabledNaNNaNNaNNaNNaNNaNNaN
4093Show Suppressed: FalseNaNNaNNaNNaNNaNNaNNaN
4094---NaNNaNNaNNaNNaNNaNNaN
4095Help: See http://wonder.cdc.gov/wonder/help/uc...NaNNaNNaNNaNNaNNaNNaN
4096---NaNNaNNaNNaNNaNNaNNaN
4097Suggested Citation: Centers for Disease Contro...NaNNaNNaNNaNNaNNaNNaN
40981999-2017 on CDC WONDER Online Database, relea...NaNNaNNaNNaNNaNNaNNaN
4099compiled from data provided by the 57 vital st...NaNNaNNaNNaNNaNNaNNaN
4100at http://wonder.cdc.gov/ucd-icd10.html on Oct...NaNNaNNaNNaNNaNNaNNaN
4101---NaNNaNNaNNaNNaNNaNNaN
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CountyCounty CodeYearYear CodeDrug/Alcohol Induced CauseDrug/Alcohol Induced Cause CodeDeaths
32856Sequoyah County, OK40135.02010.02010.0Drug poisonings (overdose) Unintentional (X40-...D111.0
11096Stark County, OH39151.02005.02005.0Drug poisonings (overdose) Unintentional (X40-...D116.0
48071Butte County, CA6007.02014.02014.0All other alcohol-induced causesA942.0
2822Garfield County, OK40047.02003.02003.0All other non-drug and non-alcohol causesO9676.0
35841Terrebonne Parish, LA22109.02011.02011.0All other non-drug and non-alcohol causesO9952.0
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CountyCounty CodeYearDrug/Alcohol Induced CauseDeaths
30939Wabash County, IN181692010Drug poisonings (overdose) Unintentional (X40-...10
36770Cayuga County, NY360112011Drug poisonings (overdose) Unintentional (X40-...10
2317Passaic County, NJ340312003Drug poisonings (overdose) Unintentional (X40-...28
9869Frederick County, MD240212005Drug poisonings (overdose) Undetermined (Y10-Y14)13
22426Boyd County, KY210192008Drug poisonings (overdose) Unintentional (X40-...24
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CountyCounty_CodeYearCauseDeathsBUYER_COUNTYBUYER_STATEcountyfips_merge
1799Hidalgo County, TX482152005All other drug-induced causes10HIDALGOTX48215both
9215Greenville County, SC450452014Drug poisonings (overdose) Suicide (X60-X64)10GREENVILLESC45045both
2843St. Clair County, IL171632007Drug poisonings (overdose) Unintentional (X40-...15SAINT CLAIRIL17163both
1063Northampton County, PA420952004Drug poisonings (overdose) Unintentional (X40-...16NORTHAMPTONPA42095both
4368Newton County, GA132172009Drug poisonings (overdose) Unintentional (X40-...13NEWTONGA13217both
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
State_CodeCountyCounty_CodeYearCauseDeaths
2201MIBERRIEN260212006Drug poisonings (overdose) Unintentional (X40-...21
3238TNBRADLEY470112007Drug poisonings (overdose) Unintentional (X40-...23
8909MSMADISON280892014Drug poisonings (overdose) Unintentional (X40-...12
7578AZMARICOPA040132013Drug poisonings (overdose) Undetermined (Y10-Y14)89
5624SCOCONEE450732010Drug poisonings (overdose) Unintentional (X40-...16
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
stateabbrevcode
13IllinoisIll.IL
40South CarolinaS.C.SC
10GeorgiaGa.GA
34North DakotaN.D.ND
38PennsylvaniaPa.PA
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
State_CodeCountyCounty_CodeYearCauseDeathsState_merge
6372OHDELAWARE390412011Drug poisonings (overdose) Unintentional (X40-...11Ohioboth
5339MIWASHTENAW261612010Drug poisonings (overdose) Undetermined (Y10-Y14)19Michiganboth
8433WVWAYNE540992013Drug poisonings (overdose) Unintentional (X40-...17West Virginiaboth
4737ORCLACKAMAS410052009Drug poisonings (overdose) Unintentional (X40-...32Oregonboth
93COLARIMER080692003Drug poisonings (overdose) Unintentional (X40-...13Coloradoboth
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
StateState_CodeCountyCounty_CodeYearCauseDeaths
2398OhioOHTRUMBULL391552006Drug poisonings (overdose) Unintentional (X40-...30
5703UtahUTDAVIS490112010Drug poisonings (overdose) Unintentional (X40-...22
10208South CarolinaSCAIKEN450032015Drug poisonings (overdose) Unintentional (X40-...30
242MarylandMDPRINCE GEORGES240332003Drug poisonings (overdose) Unintentional (X40-...14
1250CaliforniaCAMARIN060412005Drug poisonings (overdose) Undetermined (Y10-Y14)16
\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 }