{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Export Dataset to CSV" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook shows an example of how to export a dataset to a csv file. \n", "It assumes you found the dataset using techniques shown in `finding_datasets.ipynb` and loaded the dataset using `loading_datasets.ipynb`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import openpolicedata as opd" ] }, { "cell_type": "code", "execution_count": 2, "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", "
StateSourceNameAgencyAgencyFullTableTypecoverage_startcoverage_endlast_coverage_checkDescriptionsource_urlreadmeURLYearDataTypedate_fielddataset_idagency_fieldmin_versionquery
480MarylandMontgomery CountyMontgomery CountyMontgomery County Police DepartmentCOMPLAINTS2013-10-242024-05-0605/10/2024This dataset contains allegations brought to t...https://data.montgomerycountymd.gov/Public-Saf...<NA>data.montgomerycountymd.govMULTIPLESocratacreated_dtusip-62e2<NA><NA>NaN
481MarylandMontgomery CountyMontgomery CountyMontgomery County Police DepartmentCRASHES - INCIDENTS2015-12-202024-01-0305/10/2024general information about each collision and d...https://data.montgomerycountymd.gov/Public-Saf...<NA>data.montgomerycountymd.govMULTIPLESocratacrash_date_timebhju-22kf<NA>0.4NaN
482MarylandMontgomery CountyMontgomery CountyMontgomery County Police DepartmentCRASHES - NONMOTORIST2015-03-232023-12-3105/10/2024information on non-motorists (pedestrians and ...https://data.montgomerycountymd.gov/Public-Saf...<NA>data.montgomerycountymd.govMULTIPLESocratacrash_date_timen7fk-dce5<NA>0.5NaN
483MarylandMontgomery CountyMontgomery CountyMontgomery County Police DepartmentCRASHES - SUBJECTS2015-06-302024-01-0305/10/2024information on motor vehicle operators (driver...https://data.montgomerycountymd.gov/Public-Saf...<NA>data.montgomerycountymd.govMULTIPLESocratacrash_date_timemmzv-x632<NA>0.4NaN
484MarylandMontgomery CountyMontgomery CountyMontgomery County Police DepartmentINCIDENTS2017-04-022024-05-1005/10/2024list of Police Dispatched Incidents recordshttps://data.montgomerycountymd.gov/Public-Saf...<NA>data.montgomerycountymd.govMULTIPLESocratastart_time98cc-bc7d<NA><NA>NaN
\n", "
" ], "text/plain": [ " State SourceName Agency \\\n", "480 Maryland Montgomery County Montgomery County \n", "481 Maryland Montgomery County Montgomery County \n", "482 Maryland Montgomery County Montgomery County \n", "483 Maryland Montgomery County Montgomery County \n", "484 Maryland Montgomery County Montgomery County \n", "\n", " AgencyFull TableType \\\n", "480 Montgomery County Police Department COMPLAINTS \n", "481 Montgomery County Police Department CRASHES - INCIDENTS \n", "482 Montgomery County Police Department CRASHES - NONMOTORIST \n", "483 Montgomery County Police Department CRASHES - SUBJECTS \n", "484 Montgomery County Police Department INCIDENTS \n", "\n", " coverage_start coverage_end last_coverage_check \\\n", "480 2013-10-24 2024-05-06 05/10/2024 \n", "481 2015-12-20 2024-01-03 05/10/2024 \n", "482 2015-03-23 2023-12-31 05/10/2024 \n", "483 2015-06-30 2024-01-03 05/10/2024 \n", "484 2017-04-02 2024-05-10 05/10/2024 \n", "\n", " Description \\\n", "480 This dataset contains allegations brought to t... \n", "481 general information about each collision and d... \n", "482 information on non-motorists (pedestrians and ... \n", "483 information on motor vehicle operators (driver... \n", "484 list of Police Dispatched Incidents records \n", "\n", " source_url readme \\\n", "480 https://data.montgomerycountymd.gov/Public-Saf... \n", "481 https://data.montgomerycountymd.gov/Public-Saf... \n", "482 https://data.montgomerycountymd.gov/Public-Saf... \n", "483 https://data.montgomerycountymd.gov/Public-Saf... \n", "484 https://data.montgomerycountymd.gov/Public-Saf... \n", "\n", " URL Year DataType date_field \\\n", "480 data.montgomerycountymd.gov MULTIPLE Socrata created_dt \n", "481 data.montgomerycountymd.gov MULTIPLE Socrata crash_date_time \n", "482 data.montgomerycountymd.gov MULTIPLE Socrata crash_date_time \n", "483 data.montgomerycountymd.gov MULTIPLE Socrata crash_date_time \n", "484 data.montgomerycountymd.gov MULTIPLE Socrata start_time \n", "\n", " dataset_id agency_field min_version query \n", "480 usip-62e2 NaN \n", "481 bhju-22kf 0.4 NaN \n", "482 n7fk-dce5 0.5 NaN \n", "483 mmzv-x632 0.4 NaN \n", "484 98cc-bc7d NaN " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# To access the data, create a source using a Source Name (usually a police department name). There is an optional state input to clarify ambiguities.\n", "# We will use the above cell's information for Maryland to choose the agency \"Montgomery County\" which we select for the source_name\n", "\n", "src = opd.Source(source_name=\"Montgomery County\", state=\"Maryland\")\n", "src.datasets.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Load traffic stop data for 2021\n", "t = src.load(table_type='TRAFFIC STOPS', year=2021)" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
geometryseq_iddate_of_stoptime_of_stopagencysubagencydescriptionlocationlatitudelongitude...driver_statedl_statearrest_typesearch_conductedsearch_outcomesearch_reason_for_stopsearch_dispositionsearch_reasonsearch_typesearch_arrest_reason
0POINT (-77.13047 39.01268)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaRECKLESS DRIVING VEHICLE IN WANTON AND WILLFUL...IFO 9609 SINGLETON DR39.0126813333333-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
1POINT (-77.13047 39.01268)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaFAILURE OF VEH. DRIVER IN ACCIDENT TO LOCATE A...IFO 9609 SINGLETON DR39.0126813333333-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
2POINT (-77.13047 39.01268)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaNEGLIGENT DRIVING VEHICLE IN CARELESS AND IMPR...IFO 9609 SINGLETON DR39.0126813333333-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
3POINT (-77.13047 39.01268)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaFAILURE OF VEH. DRIVER TO STOP AFTER UNATTENDE...IFO 9609 SINGLETON DR39.0126813333333-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
4POINT (-77.13047 39.01268)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaFAILURE OF VEH. DRIVER INVOLVED IN ACCIDENT TO...IFO 9609 SINGLETON DR39.0126813333333-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
\n", "

5 rows × 43 columns

\n", "
" ], "text/plain": [ " geometry seq_id \\\n", "0 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "1 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "2 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "3 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "4 POINT (-77.13047 39.01268) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "\n", " date_of_stop time_of_stop agency subagency \\\n", "0 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "1 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "2 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "3 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "4 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "\n", " description location \\\n", "0 RECKLESS DRIVING VEHICLE IN WANTON AND WILLFUL... IFO 9609 SINGLETON DR \n", "1 FAILURE OF VEH. DRIVER IN ACCIDENT TO LOCATE A... IFO 9609 SINGLETON DR \n", "2 NEGLIGENT DRIVING VEHICLE IN CARELESS AND IMPR... IFO 9609 SINGLETON DR \n", "3 FAILURE OF VEH. DRIVER TO STOP AFTER UNATTENDE... IFO 9609 SINGLETON DR \n", "4 FAILURE OF VEH. DRIVER INVOLVED IN ACCIDENT TO... IFO 9609 SINGLETON DR \n", "\n", " latitude longitude ... driver_state dl_state arrest_type \\\n", "0 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol \n", "1 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol \n", "2 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol \n", "3 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol \n", "4 39.0126813333333 -77.130466 ... MD MD A - Marked Patrol \n", "\n", " search_conducted search_outcome search_reason_for_stop search_disposition \\\n", "0 NaN NaN NaN NaN \n", "1 NaN NaN NaN NaN \n", "2 NaN NaN NaN NaN \n", "3 NaN NaN NaN NaN \n", "4 NaN NaN NaN NaN \n", "\n", " search_reason search_type search_arrest_reason \n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 NaN NaN NaN \n", "3 NaN NaN NaN \n", "4 NaN NaN NaN \n", "\n", "[5 rows x 43 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show the first 5 rows of the table\n", "t.table.head(n=5)\n", "# Now you are ready for analyzing the data in the table t." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The CSV file will be written to c:\\Users\\matth\\repos\\openpolicedata\\docs\\source\\examples\\opd-examples. Make sure this path is okay before running the next cell. If the path is not okay then modify csv_filepath.\n" ] } ], "source": [ "import os\n", "cwd = os.getcwd()\n", "csv_filepath = cwd\n", "\n", "print(f\"The CSV file will be written to {csv_filepath}. Make sure this path is okay before running the next cell. If the path is not okay then modify csv_filepath.\")\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The CSV file was written to c:\\Users\\matth\\repos\\openpolicedata\\docs\\source\\examples\\opd-examples\\Maryland_Montgomery_County_TRAFFIC_STOPS_2021.csv.\n" ] } ], "source": [ "# Save to CSV. To specify a custom filename, set the filename input\n", "csv_written_filename=t.to_csv(output_dir=csv_filepath)\n", "print(f\"The CSV file was written to {csv_written_filename}.\")" ] }, { "cell_type": "code", "execution_count": 7, "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", "
geometryseq_iddate_of_stoptime_of_stopagencysubagencydescriptionlocationlatitudelongitude...driver_statedl_statearrest_typesearch_conductedsearch_outcomesearch_reason_for_stopsearch_dispositionsearch_reasonsearch_typesearch_arrest_reason
0POINT (-77.130466 39.0126813333333)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaRECKLESS DRIVING VEHICLE IN WANTON AND WILLFUL...IFO 9609 SINGLETON DR39.012681-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
1POINT (-77.130466 39.0126813333333)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaFAILURE OF VEH. DRIVER IN ACCIDENT TO LOCATE A...IFO 9609 SINGLETON DR39.012681-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
2POINT (-77.130466 39.0126813333333)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaNEGLIGENT DRIVING VEHICLE IN CARELESS AND IMPR...IFO 9609 SINGLETON DR39.012681-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
3POINT (-77.130466 39.0126813333333)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaFAILURE OF VEH. DRIVER TO STOP AFTER UNATTENDE...IFO 9609 SINGLETON DR39.012681-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
4POINT (-77.130466 39.0126813333333)f08d0293-6ade-4802-84c1-4b7b1a7072452021-01-0103:12:00MCP2nd District, BethesdaFAILURE OF VEH. DRIVER INVOLVED IN ACCIDENT TO...IFO 9609 SINGLETON DR39.012681-77.130466...MDMDA - Marked PatrolNaNNaNNaNNaNNaNNaNNaN
\n", "

5 rows × 43 columns

\n", "
" ], "text/plain": [ " geometry seq_id \\\n", "0 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "1 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "2 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "3 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "4 POINT (-77.130466 39.0126813333333) f08d0293-6ade-4802-84c1-4b7b1a707245 \n", "\n", " date_of_stop time_of_stop agency subagency \\\n", "0 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "1 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "2 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "3 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "4 2021-01-01 03:12:00 MCP 2nd District, Bethesda \n", "\n", " description location \\\n", "0 RECKLESS DRIVING VEHICLE IN WANTON AND WILLFUL... IFO 9609 SINGLETON DR \n", "1 FAILURE OF VEH. DRIVER IN ACCIDENT TO LOCATE A... IFO 9609 SINGLETON DR \n", "2 NEGLIGENT DRIVING VEHICLE IN CARELESS AND IMPR... IFO 9609 SINGLETON DR \n", "3 FAILURE OF VEH. DRIVER TO STOP AFTER UNATTENDE... IFO 9609 SINGLETON DR \n", "4 FAILURE OF VEH. DRIVER INVOLVED IN ACCIDENT TO... IFO 9609 SINGLETON DR \n", "\n", " latitude longitude ... driver_state dl_state arrest_type \\\n", "0 39.012681 -77.130466 ... MD MD A - Marked Patrol \n", "1 39.012681 -77.130466 ... MD MD A - Marked Patrol \n", "2 39.012681 -77.130466 ... MD MD A - Marked Patrol \n", "3 39.012681 -77.130466 ... MD MD A - Marked Patrol \n", "4 39.012681 -77.130466 ... MD MD A - Marked Patrol \n", "\n", " search_conducted search_outcome search_reason_for_stop search_disposition \\\n", "0 NaN NaN NaN NaN \n", "1 NaN NaN NaN NaN \n", "2 NaN NaN NaN NaN \n", "3 NaN NaN NaN NaN \n", "4 NaN NaN NaN NaN \n", "\n", " search_reason search_type search_arrest_reason \n", "0 NaN NaN NaN \n", "1 NaN NaN NaN \n", "2 NaN NaN NaN \n", "3 NaN NaN NaN \n", "4 NaN NaN NaN \n", "\n", "[5 rows x 43 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# To load data back in from CSV, create a new source and use load_from_csv\n", "# load_from_csv usage is similar to load except for the output_dir\n", "# input\n", "src = opd.Source(source_name=\"Montgomery County\", state=\"Maryland\")\n", "t = src.load_from_csv(year=2021, table_type='TRAFFIC STOPS', output_dir=csv_filepath)\n", "t.table.head()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.12 ('opd')", "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.9.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "a73158d29711b2da05ac73de25b71e5d8cae591f14917bba77a9573b5c85a0ce" } } }, "nbformat": 4, "nbformat_minor": 2 }