Datasets:
File size: 54,187 Bytes
6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 b1a5491 6a0acc8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 |
{
"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": [
"<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>Notes</th>\n",
" <th>County</th>\n",
" <th>County Code</th>\n",
" <th>Year</th>\n",
" <th>Year Code</th>\n",
" <th>Drug/Alcohol Induced Cause</th>\n",
" <th>Drug/Alcohol Induced Cause Code</th>\n",
" <th>Deaths</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>968</th>\n",
" <td>NaN</td>\n",
" <td>Pope County, IL</td>\n",
" <td>17151.0</td>\n",
" <td>2003.0</td>\n",
" <td>2003.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>34.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2479</th>\n",
" <td>NaN</td>\n",
" <td>Washington County, NY</td>\n",
" <td>36115.0</td>\n",
" <td>2003.0</td>\n",
" <td>2003.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>640.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1098</th>\n",
" <td>NaN</td>\n",
" <td>Warren County, IN</td>\n",
" <td>18171.0</td>\n",
" <td>2003.0</td>\n",
" <td>2003.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>91.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>302</th>\n",
" <td>NaN</td>\n",
" <td>Riverside County, CA</td>\n",
" <td>6065.0</td>\n",
" <td>2003.0</td>\n",
" <td>2003.0</td>\n",
" <td>All other alcohol-induced causes</td>\n",
" <td>A9</td>\n",
" <td>210.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3945</th>\n",
" <td>NaN</td>\n",
" <td>Monongalia County, WV</td>\n",
" <td>54061.0</td>\n",
" <td>2003.0</td>\n",
" <td>2003.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>579.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<class 'pandas.core.frame.DataFrame'>\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": [
"<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>Notes</th>\n",
" <th>County</th>\n",
" <th>County Code</th>\n",
" <th>Year</th>\n",
" <th>Year Code</th>\n",
" <th>Drug/Alcohol Induced Cause</th>\n",
" <th>Drug/Alcohol Induced Cause Code</th>\n",
" <th>Deaths</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>4087</th>\n",
" <td>---</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>4088</th>\n",
" <td>Dataset: Underlying Cause of Death, 1999-2017</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>4089</th>\n",
" <td>Query Parameters:</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>4090</th>\n",
" <td>Group By: County; Year; Drug/Alcohol Induced C...</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>4091</th>\n",
" <td>Show Totals: Disabled</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>4092</th>\n",
" <td>Show Zero Values: Disabled</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>4093</th>\n",
" <td>Show Suppressed: False</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>4094</th>\n",
" <td>---</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>4095</th>\n",
" <td>Help: See http://wonder.cdc.gov/wonder/help/uc...</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>4096</th>\n",
" <td>---</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>4097</th>\n",
" <td>Suggested Citation: Centers for Disease Contro...</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>4098</th>\n",
" <td>1999-2017 on CDC WONDER Online Database, relea...</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>4099</th>\n",
" <td>compiled from data provided by the 57 vital st...</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>4100</th>\n",
" <td>at http://wonder.cdc.gov/ucd-icd10.html on Oct...</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>4101</th>\n",
" <td>---</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",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<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>County</th>\n",
" <th>County Code</th>\n",
" <th>Year</th>\n",
" <th>Year Code</th>\n",
" <th>Drug/Alcohol Induced Cause</th>\n",
" <th>Drug/Alcohol Induced Cause Code</th>\n",
" <th>Deaths</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>31806</th>\n",
" <td>Jones County, MS</td>\n",
" <td>28067.0</td>\n",
" <td>2010.0</td>\n",
" <td>2010.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>629.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34546</th>\n",
" <td>Siskiyou County, CA</td>\n",
" <td>6093.0</td>\n",
" <td>2011.0</td>\n",
" <td>2011.0</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>D1</td>\n",
" <td>12.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>53754</th>\n",
" <td>Tipton County, IN</td>\n",
" <td>18159.0</td>\n",
" <td>2015.0</td>\n",
" <td>2015.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>158</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6195</th>\n",
" <td>Wright County, MO</td>\n",
" <td>29229.0</td>\n",
" <td>2004.0</td>\n",
" <td>2004.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>201.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18249</th>\n",
" <td>De Soto Parish, LA</td>\n",
" <td>22031.0</td>\n",
" <td>2007.0</td>\n",
" <td>2007.0</td>\n",
" <td>All other non-drug and non-alcohol causes</td>\n",
" <td>O9</td>\n",
" <td>270.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<class 'pandas.core.frame.DataFrame'>\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": [
"<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>County</th>\n",
" <th>County Code</th>\n",
" <th>Year</th>\n",
" <th>Drug/Alcohol Induced Cause</th>\n",
" <th>Deaths</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>56363</th>\n",
" <td>Bexar County, TX</td>\n",
" <td>48029</td>\n",
" <td>2015</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>179</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43378</th>\n",
" <td>Yuma County, AZ</td>\n",
" <td>04027</td>\n",
" <td>2013</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>32694</th>\n",
" <td>Lake County, OH</td>\n",
" <td>39085</td>\n",
" <td>2010</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>39</td>\n",
" </tr>\n",
" <tr>\n",
" <th>37892</th>\n",
" <td>Collin County, TX</td>\n",
" <td>48085</td>\n",
" <td>2011</td>\n",
" <td>Drug poisonings (overdose) Suicide (X60-X64)</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10028</th>\n",
" <td>Oakland County, MI</td>\n",
" <td>26125</td>\n",
" <td>2005</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>25</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<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>County</th>\n",
" <th>County_Code</th>\n",
" <th>Year</th>\n",
" <th>Cause</th>\n",
" <th>Deaths</th>\n",
" <th>BUYER_COUNTY</th>\n",
" <th>BUYER_STATE</th>\n",
" <th>countyfips</th>\n",
" <th>_merge</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>10414</th>\n",
" <td>Jefferson County, WI</td>\n",
" <td>55055</td>\n",
" <td>2015</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>10</td>\n",
" <td>JEFFERSON</td>\n",
" <td>WI</td>\n",
" <td>55055</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7347</th>\n",
" <td>Delaware County, PA</td>\n",
" <td>42045</td>\n",
" <td>2012</td>\n",
" <td>Drug poisonings (overdose) Undetermined (Y10-Y14)</td>\n",
" <td>16</td>\n",
" <td>DELAWARE</td>\n",
" <td>PA</td>\n",
" <td>42045</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5185</th>\n",
" <td>Williamson County, IL</td>\n",
" <td>17199</td>\n",
" <td>2010</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>11</td>\n",
" <td>WILLIAMSON</td>\n",
" <td>IL</td>\n",
" <td>17199</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10403</th>\n",
" <td>Putnam County, WV</td>\n",
" <td>54079</td>\n",
" <td>2015</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>19</td>\n",
" <td>PUTNAM</td>\n",
" <td>WV</td>\n",
" <td>54079</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5365</th>\n",
" <td>Jackson County, MO</td>\n",
" <td>29095</td>\n",
" <td>2010</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>98</td>\n",
" <td>JACKSON</td>\n",
" <td>MO</td>\n",
" <td>29095</td>\n",
" <td>both</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<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>State_Code</th>\n",
" <th>County</th>\n",
" <th>County_Code</th>\n",
" <th>Year</th>\n",
" <th>Cause</th>\n",
" <th>Deaths</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>4529</th>\n",
" <td>MI</td>\n",
" <td>MACOMB</td>\n",
" <td>26099</td>\n",
" <td>2009</td>\n",
" <td>Drug poisonings (overdose) Undetermined (Y10-Y14)</td>\n",
" <td>29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8646</th>\n",
" <td>FL</td>\n",
" <td>PASCO</td>\n",
" <td>12101</td>\n",
" <td>2014</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>95</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6511</th>\n",
" <td>TN</td>\n",
" <td>DAVIDSON</td>\n",
" <td>47037</td>\n",
" <td>2011</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>91</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9042</th>\n",
" <td>NC</td>\n",
" <td>HARNETT</td>\n",
" <td>37085</td>\n",
" <td>2014</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8700</th>\n",
" <td>ID</td>\n",
" <td>ADA</td>\n",
" <td>16001</td>\n",
" <td>2014</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>50</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<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>state</th>\n",
" <th>abbrev</th>\n",
" <th>code</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>Mississippi</td>\n",
" <td>Miss.</td>\n",
" <td>MS</td>\n",
" </tr>\n",
" <tr>\n",
" <th>48</th>\n",
" <td>West Virginia</td>\n",
" <td>W.Va.</td>\n",
" <td>WV</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>California</td>\n",
" <td>Calif.</td>\n",
" <td>CA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31</th>\n",
" <td>New Mexico</td>\n",
" <td>N.M.</td>\n",
" <td>NM</td>\n",
" </tr>\n",
" <tr>\n",
" <th>45</th>\n",
" <td>Vermont</td>\n",
" <td>Vt.</td>\n",
" <td>VT</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<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>State_Code</th>\n",
" <th>County</th>\n",
" <th>County_Code</th>\n",
" <th>Year</th>\n",
" <th>Cause</th>\n",
" <th>Deaths</th>\n",
" <th>State</th>\n",
" <th>_merge</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1295</th>\n",
" <td>CA</td>\n",
" <td>VENTURA</td>\n",
" <td>06111</td>\n",
" <td>2005</td>\n",
" <td>Drug poisonings (overdose) Suicide (X60-X64)</td>\n",
" <td>16</td>\n",
" <td>California</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7659</th>\n",
" <td>CA</td>\n",
" <td>SONOMA</td>\n",
" <td>06097</td>\n",
" <td>2013</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>31</td>\n",
" <td>California</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9290</th>\n",
" <td>TX</td>\n",
" <td>MONTGOMERY</td>\n",
" <td>48339</td>\n",
" <td>2014</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>31</td>\n",
" <td>Texas</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5006</th>\n",
" <td>CA</td>\n",
" <td>ORANGE</td>\n",
" <td>06059</td>\n",
" <td>2010</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>234</td>\n",
" <td>California</td>\n",
" <td>both</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8888</th>\n",
" <td>MI</td>\n",
" <td>SAGINAW</td>\n",
" <td>26145</td>\n",
" <td>2014</td>\n",
" <td>Drug poisonings (overdose) Undetermined (Y10-Y14)</td>\n",
" <td>36</td>\n",
" <td>Michigan</td>\n",
" <td>both</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
"<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>State</th>\n",
" <th>State_Code</th>\n",
" <th>County</th>\n",
" <th>County_Code</th>\n",
" <th>Year</th>\n",
" <th>Cause</th>\n",
" <th>Deaths</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2496</th>\n",
" <td>Tennessee</td>\n",
" <td>TN</td>\n",
" <td>KNOX</td>\n",
" <td>47093</td>\n",
" <td>2006</td>\n",
" <td>Drug poisonings (overdose) Undetermined (Y10-Y14)</td>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2458</th>\n",
" <td>Pennsylvania</td>\n",
" <td>PA</td>\n",
" <td>PHILADELPHIA</td>\n",
" <td>42101</td>\n",
" <td>2006</td>\n",
" <td>Drug poisonings (overdose) Suicide (X60-X64)</td>\n",
" <td>17</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7091</th>\n",
" <td>Michigan</td>\n",
" <td>MI</td>\n",
" <td>VAN BUREN</td>\n",
" <td>26159</td>\n",
" <td>2012</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5421</th>\n",
" <td>New Mexico</td>\n",
" <td>NM</td>\n",
" <td>SANTA FE</td>\n",
" <td>35049</td>\n",
" <td>2010</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3975</th>\n",
" <td>Pennsylvania</td>\n",
" <td>PA</td>\n",
" <td>LACKAWANNA</td>\n",
" <td>42069</td>\n",
" <td>2008</td>\n",
" <td>Drug poisonings (overdose) Unintentional (X40-...</td>\n",
" <td>37</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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
}
|