{"cells":[{"cell_type":"markdown","id":"70b5cc9a-d1a4-4ce4-8c0a-e4450d565a90","metadata":{},"outputs":[],"source":["<center>\n","    <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DB0201EN-SkillsNetwork/labs/FinalModule_Coursera_V5/images/SN_web_lightmode.png\" width=\"300\" alt=\"cognitiveclass.ai logo\">\n","</center>\n","\n","<h1 align=center><font size = 5>Assignment: Notebook for Graded Assessment</font></h1>\n"]},{"cell_type":"markdown","id":"a73867d0-d5e3-4718-8489-6a62ac5eaaff","metadata":{},"outputs":[],"source":["# Introduction\n","\n","Using this Python notebook you will:\n","\n","1.  Understand three Chicago datasets\n","2.  Load the three datasets into three tables in a SQLIte database\n","3.  Execute SQL queries to answer assignment questions\n"]},{"cell_type":"markdown","id":"f2f4f1bc-b587-4b85-bf3b-3828d9dc7d0e","metadata":{},"outputs":[],"source":["## Understand the datasets\n","\n","To complete the assignment problems in this notebook you will be using three datasets that are available on the city of Chicago's Data Portal:\n","\n","1.  <a href=\"https://data.cityofchicago.org/Health-Human-Services/Census-Data-Selected-socioeconomic-indicators-in-C/kn9c-c2s2?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01\">Socioeconomic Indicators in Chicago</a>\n","2.  <a href=\"https://data.cityofchicago.org/Education/Chicago-Public-Schools-Progress-Report-Cards-2011-/9xs2-f89t?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01\">Chicago Public Schools</a>\n","3.  <a href=\"https://data.cityofchicago.org/Public-Safety/Crimes-2001-to-present/ijzp-q8t2?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01\">Chicago Crime Data</a>\n","\n","### 1. Socioeconomic Indicators in Chicago\n","\n","This dataset contains a selection of six socioeconomic indicators of public health significance and a “hardship index,” for each Chicago community area, for the years 2008 – 2012.\n","\n","A detailed description of this dataset and the original dataset can be obtained from the Chicago Data Portal at:\n","\n","[https://data.cityofchicago.org/Health-Human-Services/Census-Data-Selected-socioeconomic-indicators-in-C/kn9c-c2s2](https://data.cityofchicago.org/Health-Human-Services/Census-Data-Selected-socioeconomic-indicators-in-C/kn9c-c2s2?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01&cm_mmc=Email_Newsletter-\\_-Developer_Ed%2BTech-\\_-WW_WW-\\_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-DB0201EN-SkillsNetwork-20127838&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ)\n","\n","### 2. Chicago Public Schools\n","\n","This dataset shows all school level performance data used to create CPS School Report Cards for the 2011-2012 school year. This dataset is provided by the city of Chicago's Data Portal.\n","\n","A detailed description of this dataset and the original dataset can be obtained from the Chicago Data Portal at:\n","\n","[https://data.cityofchicago.org/Education/Chicago-Public-Schools-Progress-Report-Cards-2011-/9xs2-f89t](https://data.cityofchicago.org/Education/Chicago-Public-Schools-Progress-Report-Cards-2011-/9xs2-f89t?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01&cm_mmc=Email_Newsletter-\\_-Developer_Ed%2BTech-\\_-WW_WW-\\_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-DB0201EN-SkillsNetwork-20127838&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ)\n","\n","### 3. Chicago Crime Data\n","\n","This dataset reflects reported incidents of crime (with the exception of murders where data exists for each victim) that occurred in the City of Chicago from 2001 to present, minus the most recent seven days.\n","\n","A detailed description of this dataset and the original dataset can be obtained from the Chicago Data Portal at:\n","\n","[https://data.cityofchicago.org/Public-Safety/Crimes-2001-to-present/ijzp-q8t2](https://data.cityofchicago.org/Public-Safety/Crimes-2001-to-present/ijzp-q8t2?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01&cm_mmc=Email_Newsletter-\\_-Developer_Ed%2BTech-\\_-WW_WW-\\_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-DB0201EN-SkillsNetwork-20127838&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ)\n"]},{"cell_type":"markdown","id":"05b102c6-4f33-4d8d-85d0-861dc959c589","metadata":{},"outputs":[],"source":["### Download the datasets\n","\n","This assignment requires you to have these three tables populated with a subset of the whole datasets.\n","\n","In many cases the dataset to be analyzed is available as a .CSV (comma separated values) file, perhaps on the internet. \n","\n","Use the links below to read the data files using the Pandas library. \n","\n","* Chicago Census Data\n","\n","https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DB0201EN-SkillsNetwork/labs/FinalModule_Coursera_V5/data/ChicagoCensusData.csv?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01\n","\n","* Chicago Public Schools\n","\n","https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DB0201EN-SkillsNetwork/labs/FinalModule_Coursera_V5/data/ChicagoPublicSchools.csv?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01\n","\n","* Chicago Crime Data\n","\n","https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DB0201EN-SkillsNetwork/labs/FinalModule_Coursera_V5/data/ChicagoCrimeData.csv?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkDB0201ENSkillsNetwork20127838-2021-01-01\n","\n","**NOTE:** Ensure you use the datasets available on the links above instead of directly from the Chicago Data Portal. The versions linked here are subsets of the original datasets and have some of the column names modified to be more database friendly which will make it easier to complete this assignment.\n"]},{"cell_type":"markdown","id":"fe3df6d7-0779-458c-b5a0-77fc7b46199c","metadata":{},"outputs":[],"source":["Execute the below code cell to install the required libraries\n"]},{"cell_type":"code","id":"c316cd18-018f-4700-a494-f19d8b0a84b7","metadata":{},"outputs":[],"source":["!pip install pandas\n!pip install ipython-sql prettytable \n\nimport prettytable\n\nprettytable.DEFAULT = 'DEFAULT'"]},{"cell_type":"markdown","id":"16958cea-b2f2-4103-8708-1540b02330e4","metadata":{},"outputs":[],"source":["### Store the datasets in database tables\n","\n","To analyze the data using SQL, it first needs to be loaded into SQLite DB.\n","We will create three tables in as under:\n","\n","1.  **CENSUS_DATA**\n","2.  **CHICAGO_PUBLIC_SCHOOLS**\n","3.  **CHICAGO_CRIME_DATA**\n"]},{"cell_type":"markdown","id":"b9fef037-5c36-4d05-9125-03a082630ec2","metadata":{},"outputs":[],"source":["Load the `pandas` and `sqlite3` libraries and establish a connection to `FinalDB.db`\n"]},{"cell_type":"code","id":"8505847a-b0ed-4b87-b169-309583e3dbcd","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"75063743-8ae8-4900-ba3d-d2525c6ad3ec","metadata":{},"outputs":[],"source":["Load the SQL magic module\n"]},{"cell_type":"code","id":"ebf8b224-1c5f-4e68-852f-587c4149ff57","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"12c39c45-e8c9-4e90-b864-eadc43e84bf6","metadata":{},"outputs":[],"source":["Use `Pandas` to load the data available in the links above to dataframes. Use these dataframes to load data on to the database `FinalDB.db` as required tables.\n"]},{"cell_type":"code","id":"114f9e25-129a-4fe1-a775-69725f37020f","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"a0a61679-546a-44a7-a8e6-57098515df40","metadata":{},"outputs":[],"source":["Establish a connection between SQL magic module and the database `FinalDB.db`\n"]},{"cell_type":"code","id":"427a99cc-1280-400c-b820-a88419e9b156","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"72512a03-b63e-4178-9803-00d7e26e5546","metadata":{},"outputs":[],"source":["You can now proceed to the the following questions. Please note that a graded assignment will follow this lab and there will be a question on each of the problems stated below. It can be from the answer you received or the code you write for this problem. Therefore, please keep a note of both your codes as well as the response you generate.\n"]},{"cell_type":"markdown","id":"80a90d7b-839d-45d8-a222-9a34ba590698","metadata":{},"outputs":[],"source":["## Problems\n","\n","Now write and execute SQL queries to solve assignment problems\n","\n","### Problem 1\n","\n","##### Find the total number of crimes recorded in the CRIME table.\n"]},{"cell_type":"code","id":"1ae671a3-6314-4c27-8909-6212ddc61d60","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"bd317507-67ab-4d8b-ab12-e6dd23da3286","metadata":{},"outputs":[],"source":["### Problem 2\n","\n","##### List community area names and numbers with per capita income less than 11000.\n"]},{"cell_type":"code","id":"40706af1-d88e-41f3-8fc0-8df5a9197999","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"27835b18-a48b-481a-9d92-6272674a230a","metadata":{},"outputs":[],"source":["### Problem 3\n","\n","##### List all case numbers for crimes involving minors?(children are not considered minors for the purposes of crime analysis) \n"]},{"cell_type":"code","id":"031daefb-b146-4fd9-81df-074a011ca273","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"55e0cff2-a0f7-4a69-aed9-66eec9c58105","metadata":{},"outputs":[],"source":["### Problem 4\n","\n","##### List all kidnapping crimes involving a child?\n"]},{"cell_type":"code","id":"68e25d3b-62be-4ba8-b214-7d58fd8cf1ab","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"a15a622a-5473-40f1-b161-f00b34cef7b2","metadata":{},"outputs":[],"source":["### Problem 5\n","\n","##### List the kind of crimes that were recorded at schools. (No repetitions)\n"]},{"cell_type":"code","id":"4bdd58ed-3c7e-498a-9194-e5d097319002","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"1ce1ec31-382b-4c29-9115-ab0377fea9f2","metadata":{},"outputs":[],"source":["### Problem 6\n","\n","##### List the type of schools along with the average safety score for each type.\n"]},{"cell_type":"code","id":"ad1b170d-8536-4bc5-b95b-310ce9a7fe1c","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"0a2658f4-bb05-4ca1-b669-1e44c683c6df","metadata":{},"outputs":[],"source":["### Problem 7\n","\n","##### List 5 community areas with highest % of households below poverty line\n"]},{"cell_type":"code","id":"d89e667a-216f-486c-8ccb-24abdd1629e0","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"97b7fa30-8a35-4552-a0b5-bce1c2f5004a","metadata":{},"outputs":[],"source":["### Problem 8\n","\n","##### Which community area is most crime prone? Display the coumminty area number only.\n"]},{"cell_type":"code","id":"d7401d52-0773-46a6-8c49-2da2c14c5885","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"d98c3b89-c7da-4afb-837e-77a5d16a30ab","metadata":{},"outputs":[],"source":["\n","Double-click **here** for a hint\n","\n","<!--\n","Query for the 'community area number' that has most number of incidents\n","-->\n"]},{"cell_type":"markdown","id":"cece4db9-f220-46d7-a188-4cbddbf69fb4","metadata":{},"outputs":[],"source":["### Problem 9\n","\n","##### Use a sub-query to find the name of the community area with highest hardship index\n"]},{"cell_type":"code","id":"47ee236d-c67e-42b5-8f26-224cef871f7a","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"78d765df-90ad-4d11-8806-76550ed6bd38","metadata":{},"outputs":[],"source":["### Problem 10\n","\n","##### Use a sub-query to determine the Community Area Name with most number of crimes?\n"]},{"cell_type":"code","id":"e9f9a433-f3fe-484e-8a1d-df6559e0eb5a","metadata":{},"outputs":[],"source":[""]},{"cell_type":"markdown","id":"e32ccb0b-ce11-4859-b959-e59a4efc7b01","metadata":{},"outputs":[],"source":["## Author(s)\n","\n","<h4> Hima Vasudevan </h4>\n","<h4> Rav Ahuja </h4>\n","<h4> Ramesh Sannreddy </h4>\n","\n","## Contribtuor(s)\n","\n","<h4> Malika Singla </h4>\n","<h4>Abhishek Gagneja</h4>\n","<!--\n","## Change log\n","\n","| Date       | Version | Changed by        | Change Description                             |\n","| ---------- | ------- | ----------------- | ---------------------------------------------- |\n","|2023-10-18  | 2.6     | Abhishek Gagneja  | Modified instruction set |\n","| 2022-03-04 | 2.5     | Lakshmi Holla     | Changed markdown.                   |\n","| 2021-05-19 | 2.4     | Lakshmi Holla     | Updated the question                           |\n","| 2021-04-30 | 2.3     | Malika Singla     | Updated the libraries                          |\n","| 2021-01-15 | 2.2     | Rav Ahuja         | Removed problem 11 and fixed changelog         |\n","| 2020-11-25 | 2.1     | Ramesh Sannareddy | Updated the problem statements, and datasets   |\n","| 2020-09-05 | 2.0     | Malika Singla     | Moved lab to course repo in GitLab             |\n","| 2018-07-18 | 1.0     | Rav Ahuja         | Several updates including loading instructions |\n","| 2018-05-04 | 0.1     | Hima Vasudevan    | Created initial version                        |\n","-->\n","## <h3 align=\"center\"> © IBM Corporation 2023. All rights reserved. <h3/>\n"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","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.12.8"},"prev_pub_hash":"0f6ab15179bab019c866c3606fd94e4869170600ea14eaf9be65281f4517d0e2"},"nbformat":4,"nbformat_minor":4}