{ "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
968NaNPope County, IL17151.02003.02003.0All other non-drug and non-alcohol causesO934.0
2479NaNWashington County, NY36115.02003.02003.0All other non-drug and non-alcohol causesO9640.0
1098NaNWarren County, IN18171.02003.02003.0All other non-drug and non-alcohol causesO991.0
302NaNRiverside County, CA6065.02003.02003.0All other alcohol-induced causesA9210.0
3945NaNMonongalia County, WV54061.02003.02003.0All other non-drug and non-alcohol causesO9579.0
\n", "
" ], "text/plain": [ " Notes County County Code Year Year Code \\\n", "968 NaN Pope County, IL 17151.0 2003.0 2003.0 \n", "2479 NaN Washington County, NY 36115.0 2003.0 2003.0 \n", "1098 NaN Warren County, IN 18171.0 2003.0 2003.0 \n", "302 NaN Riverside County, CA 6065.0 2003.0 2003.0 \n", "3945 NaN Monongalia County, WV 54061.0 2003.0 2003.0 \n", "\n", " Drug/Alcohol Induced Cause \\\n", "968 All other non-drug and non-alcohol causes \n", "2479 All other non-drug and non-alcohol causes \n", "1098 All other non-drug and non-alcohol causes \n", "302 All other alcohol-induced causes \n", "3945 All other non-drug and non-alcohol causes \n", "\n", " Drug/Alcohol Induced Cause Code Deaths \n", "968 O9 34.0 \n", "2479 O9 640.0 \n", "1098 O9 91.0 \n", "302 A9 210.0 \n", "3945 O9 579.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
31806Jones County, MS28067.02010.02010.0All other non-drug and non-alcohol causesO9629.0
34546Siskiyou County, CA6093.02011.02011.0Drug poisonings (overdose) Unintentional (X40-...D112.0
53754Tipton County, IN18159.02015.02015.0All other non-drug and non-alcohol causesO9158
6195Wright County, MO29229.02004.02004.0All other non-drug and non-alcohol causesO9201.0
18249De Soto Parish, LA22031.02007.02007.0All other non-drug and non-alcohol causesO9270.0
\n", "
" ], "text/plain": [ " County County Code Year Year Code \\\n", "31806 Jones County, MS 28067.0 2010.0 2010.0 \n", "34546 Siskiyou County, CA 6093.0 2011.0 2011.0 \n", "53754 Tipton County, IN 18159.0 2015.0 2015.0 \n", "6195 Wright County, MO 29229.0 2004.0 2004.0 \n", "18249 De Soto Parish, LA 22031.0 2007.0 2007.0 \n", "\n", " Drug/Alcohol Induced Cause \\\n", "31806 All other non-drug and non-alcohol causes \n", "34546 Drug poisonings (overdose) Unintentional (X40-... \n", "53754 All other non-drug and non-alcohol causes \n", "6195 All other non-drug and non-alcohol causes \n", "18249 All other non-drug and non-alcohol causes \n", "\n", " Drug/Alcohol Induced Cause Code Deaths \n", "31806 O9 629.0 \n", "34546 D1 12.0 \n", "53754 O9 158 \n", "6195 O9 201.0 \n", "18249 O9 270.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
56363Bexar County, TX480292015Drug poisonings (overdose) Unintentional (X40-...179
43378Yuma County, AZ040272013Drug poisonings (overdose) Unintentional (X40-...22
32694Lake County, OH390852010Drug poisonings (overdose) Unintentional (X40-...39
37892Collin County, TX480852011Drug poisonings (overdose) Suicide (X60-X64)14
10028Oakland County, MI261252005Drug poisonings (overdose) Unintentional (X40-...25
\n", "
" ], "text/plain": [ " County County Code Year \\\n", "56363 Bexar County, TX 48029 2015 \n", "43378 Yuma County, AZ 04027 2013 \n", "32694 Lake County, OH 39085 2010 \n", "37892 Collin County, TX 48085 2011 \n", "10028 Oakland County, MI 26125 2005 \n", "\n", " Drug/Alcohol Induced Cause Deaths \n", "56363 Drug poisonings (overdose) Unintentional (X40-... 179 \n", "43378 Drug poisonings (overdose) Unintentional (X40-... 22 \n", "32694 Drug poisonings (overdose) Unintentional (X40-... 39 \n", "37892 Drug poisonings (overdose) Suicide (X60-X64) 14 \n", "10028 Drug poisonings (overdose) Unintentional (X40-... 25 " ] }, "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
10414Jefferson County, WI550552015Drug poisonings (overdose) Unintentional (X40-...10JEFFERSONWI55055both
7347Delaware County, PA420452012Drug poisonings (overdose) Undetermined (Y10-Y14)16DELAWAREPA42045both
5185Williamson County, IL171992010Drug poisonings (overdose) Unintentional (X40-...11WILLIAMSONIL17199both
10403Putnam County, WV540792015Drug poisonings (overdose) Unintentional (X40-...19PUTNAMWV54079both
5365Jackson County, MO290952010Drug poisonings (overdose) Unintentional (X40-...98JACKSONMO29095both
\n", "
" ], "text/plain": [ " County County_Code Year \\\n", "10414 Jefferson County, WI 55055 2015 \n", "7347 Delaware County, PA 42045 2012 \n", "5185 Williamson County, IL 17199 2010 \n", "10403 Putnam County, WV 54079 2015 \n", "5365 Jackson County, MO 29095 2010 \n", "\n", " Cause Deaths BUYER_COUNTY \\\n", "10414 Drug poisonings (overdose) Unintentional (X40-... 10 JEFFERSON \n", "7347 Drug poisonings (overdose) Undetermined (Y10-Y14) 16 DELAWARE \n", "5185 Drug poisonings (overdose) Unintentional (X40-... 11 WILLIAMSON \n", "10403 Drug poisonings (overdose) Unintentional (X40-... 19 PUTNAM \n", "5365 Drug poisonings (overdose) Unintentional (X40-... 98 JACKSON \n", "\n", " BUYER_STATE countyfips _merge \n", "10414 WI 55055 both \n", "7347 PA 42045 both \n", "5185 IL 17199 both \n", "10403 WV 54079 both \n", "5365 MO 29095 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
4529MIMACOMB260992009Drug poisonings (overdose) Undetermined (Y10-Y14)29
8646FLPASCO121012014Drug poisonings (overdose) Unintentional (X40-...95
6511TNDAVIDSON470372011Drug poisonings (overdose) Unintentional (X40-...91
9042NCHARNETT370852014Drug poisonings (overdose) Unintentional (X40-...14
8700IDADA160012014Drug poisonings (overdose) Unintentional (X40-...50
\n", "
" ], "text/plain": [ " State_Code County County_Code Year \\\n", "4529 MI MACOMB 26099 2009 \n", "8646 FL PASCO 12101 2014 \n", "6511 TN DAVIDSON 47037 2011 \n", "9042 NC HARNETT 37085 2014 \n", "8700 ID ADA 16001 2014 \n", "\n", " Cause Deaths \n", "4529 Drug poisonings (overdose) Undetermined (Y10-Y14) 29 \n", "8646 Drug poisonings (overdose) Unintentional (X40-... 95 \n", "6511 Drug poisonings (overdose) Unintentional (X40-... 91 \n", "9042 Drug poisonings (overdose) Unintentional (X40-... 14 \n", "8700 Drug poisonings (overdose) Unintentional (X40-... 50 " ] }, "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
24MississippiMiss.MS
48West VirginiaW.Va.WV
4CaliforniaCalif.CA
31New MexicoN.M.NM
45VermontVt.VT
\n", "
" ], "text/plain": [ " state abbrev code\n", "24 Mississippi Miss. MS\n", "48 West Virginia W.Va. WV\n", "4 California Calif. CA\n", "31 New Mexico N.M. NM\n", "45 Vermont Vt. VT" ] }, "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
1295CAVENTURA061112005Drug poisonings (overdose) Suicide (X60-X64)16Californiaboth
7659CASONOMA060972013Drug poisonings (overdose) Unintentional (X40-...31Californiaboth
9290TXMONTGOMERY483392014Drug poisonings (overdose) Unintentional (X40-...31Texasboth
5006CAORANGE060592010Drug poisonings (overdose) Unintentional (X40-...234Californiaboth
8888MISAGINAW261452014Drug poisonings (overdose) Undetermined (Y10-Y14)36Michiganboth
\n", "
" ], "text/plain": [ " State_Code County County_Code Year \\\n", "1295 CA VENTURA 06111 2005 \n", "7659 CA SONOMA 06097 2013 \n", "9290 TX MONTGOMERY 48339 2014 \n", "5006 CA ORANGE 06059 2010 \n", "8888 MI SAGINAW 26145 2014 \n", "\n", " Cause Deaths State \\\n", "1295 Drug poisonings (overdose) Suicide (X60-X64) 16 California \n", "7659 Drug poisonings (overdose) Unintentional (X40-... 31 California \n", "9290 Drug poisonings (overdose) Unintentional (X40-... 31 Texas \n", "5006 Drug poisonings (overdose) Unintentional (X40-... 234 California \n", "8888 Drug poisonings (overdose) Undetermined (Y10-Y14) 36 Michigan \n", "\n", " _merge \n", "1295 both \n", "7659 both \n", "9290 both \n", "5006 both \n", "8888 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
2496TennesseeTNKNOX470932006Drug poisonings (overdose) Undetermined (Y10-Y14)25
2458PennsylvaniaPAPHILADELPHIA421012006Drug poisonings (overdose) Suicide (X60-X64)17
7091MichiganMIVAN BUREN261592012Drug poisonings (overdose) Unintentional (X40-...10
5421New MexicoNMSANTA FE350492010Drug poisonings (overdose) Unintentional (X40-...27
3975PennsylvaniaPALACKAWANNA420692008Drug poisonings (overdose) Unintentional (X40-...37
\n", "
" ], "text/plain": [ " State State_Code County County_Code Year \\\n", "2496 Tennessee TN KNOX 47093 2006 \n", "2458 Pennsylvania PA PHILADELPHIA 42101 2006 \n", "7091 Michigan MI VAN BUREN 26159 2012 \n", "5421 New Mexico NM SANTA FE 35049 2010 \n", "3975 Pennsylvania PA LACKAWANNA 42069 2008 \n", "\n", " Cause Deaths \n", "2496 Drug poisonings (overdose) Undetermined (Y10-Y14) 25 \n", "2458 Drug poisonings (overdose) Suicide (X60-X64) 17 \n", "7091 Drug poisonings (overdose) Unintentional (X40-... 10 \n", "5421 Drug poisonings (overdose) Unintentional (X40-... 27 \n", "3975 Drug poisonings (overdose) Unintentional (X40-... 37 " ] }, "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 }