{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Summarizing OpenPoliceData Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook shows examples of the following tasks:\n", "- Summarizing available data in OpenPoliceData (OPD)\n", "- Exporting data summaries\n", "- Generating your own data summaries" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import openpolicedata as opd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The OpenPoliceData package has 425 unique datasets\n" ] } ], "source": [ "# Get The number of unique datasets (unique state, source, agency, and table type)\n", "print(f\"The OpenPoliceData package has {opd.datasets.num_unique()} unique datasets\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OPD has at least 1 datasets for all reporting agencies in 10 states\n", "OPD has at least 1 dataset for 158 individual agencies\n" ] } ], "source": [ "# Find how many datasets are available for full states and how many are available for individual agencies\n", "print(f\"OPD has at least 1 datasets for all reporting agencies in {opd.datasets.num_sources(full_states_only=True)} states\")\n", "print(f\"OPD has at least 1 dataset for {opd.datasets.num_sources()-opd.datasets.num_sources(full_states_only=True)} individual agencies\")" ] }, { "cell_type": "code", "execution_count": 5, "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", "
Total
State
California
All State Agencies2
Individual Agency58
North Carolina
All State Agencies1
Individual Agency31
New York
All State Agencies1
Individual Agency29
Arizona23
\n", "
" ], "text/plain": [ " Total\n", "State \n", "California \n", " All State Agencies 2\n", " Individual Agency 58\n", "North Carolina \n", " All State Agencies 1\n", " Individual Agency 31\n", "New York \n", " All State Agencies 1\n", " Individual Agency 29\n", "Arizona 23" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find number of datasets from each state\n", "opd.datasets.summary_by_state().head(10)" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TotalN/AMULTI-YEAR2024202320222021202020192018...2012201120102009200820072006200520042003
State
California...
All State Agencies2010011111...0000000000
Individual Agency5834247911111410...3221111000
North Carolina...
All State Agencies1010000000...0000000000
Individual Agency313260122222...2222222100
New York...
\n", "

7 rows × 25 columns

\n", "
" ], "text/plain": [ " Total N/A MULTI-YEAR 2024 2023 2022 2021 2020 2019 2018 \\\n", "State \n", "California \n", " All State Agencies 2 0 1 0 0 1 1 1 1 1 \n", " Individual Agency 58 3 42 4 7 9 11 11 14 10 \n", "North Carolina \n", " All State Agencies 1 0 1 0 0 0 0 0 0 0 \n", " Individual Agency 31 3 26 0 1 2 2 2 2 2 \n", "New York \n", "\n", " ... 2012 2011 2010 2009 2008 2007 2006 2005 2004 2003 \n", "State ... \n", "California ... \n", " All State Agencies ... 0 0 0 0 0 0 0 0 0 0 \n", " Individual Agency ... 3 2 2 1 1 1 1 0 0 0 \n", "North Carolina ... \n", " All State Agencies ... 0 0 0 0 0 0 0 0 0 0 \n", " Individual Agency ... 2 2 2 2 2 2 2 1 0 0 \n", "New York ... \n", "\n", "[7 rows x 25 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find number of datasets from each state broken down by year\n", "opd.datasets.summary_by_state(by=\"year\").head(7)" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TotalARRESTSCALLS FOR SERVICECITATIONSCOMPLAINTSCOMPLAINTS - ALLEGATIONSCOMPLAINTS - BACKGROUNDCOMPLAINTS - BODY WORN CAMERACOMPLAINTS - OFFICERSCOMPLAINTS - PENALTIES...TRAFFIC STOPSTRAFFIC STOPS - INCIDENTSTRAFFIC STOPS - SUBJECTSTRAFFIC WARNINGSUSE OF FORCEUSE OF FORCE - INCIDENTSUSE OF FORCE - OFFICERSUSE OF FORCE - SUBJECTSUSE OF FORCE - SUBJECTS/OFFICERSVEHICLE PURSUITS
State
California...
All State Agencies2000000000...0000010000
Individual Agency580103211100...7000300012
North Carolina...
All State Agencies1000000000...1000000000
Individual Agency31332100000...7110101100
New York...
\n", "

7 rows × 47 columns

\n", "
" ], "text/plain": [ " Total ARRESTS CALLS FOR SERVICE CITATIONS COMPLAINTS \\\n", "State \n", "California \n", " All State Agencies 2 0 0 0 0 \n", " Individual Agency 58 0 10 3 2 \n", "North Carolina \n", " All State Agencies 1 0 0 0 0 \n", " Individual Agency 31 3 3 2 1 \n", "New York \n", "\n", " COMPLAINTS - ALLEGATIONS COMPLAINTS - BACKGROUND \\\n", "State \n", "California \n", " All State Agencies 0 0 \n", " Individual Agency 1 1 \n", "North Carolina \n", " All State Agencies 0 0 \n", " Individual Agency 0 0 \n", "New York \n", "\n", " COMPLAINTS - BODY WORN CAMERA COMPLAINTS - OFFICERS \\\n", "State \n", "California \n", " All State Agencies 0 0 \n", " Individual Agency 1 0 \n", "North Carolina \n", " All State Agencies 0 0 \n", " Individual Agency 0 0 \n", "New York \n", "\n", " COMPLAINTS - PENALTIES ... TRAFFIC STOPS \\\n", "State ... \n", "California ... \n", " All State Agencies 0 ... 0 \n", " Individual Agency 0 ... 7 \n", "North Carolina ... \n", " All State Agencies 0 ... 1 \n", " Individual Agency 0 ... 7 \n", "New York ... \n", "\n", " TRAFFIC STOPS - INCIDENTS TRAFFIC STOPS - SUBJECTS \\\n", "State \n", "California \n", " All State Agencies 0 0 \n", " Individual Agency 0 0 \n", "North Carolina \n", " All State Agencies 0 0 \n", " Individual Agency 1 1 \n", "New York \n", "\n", " TRAFFIC WARNINGS USE OF FORCE USE OF FORCE - INCIDENTS \\\n", "State \n", "California \n", " All State Agencies 0 0 1 \n", " Individual Agency 0 3 0 \n", "North Carolina \n", " All State Agencies 0 0 0 \n", " Individual Agency 0 1 0 \n", "New York \n", "\n", " USE OF FORCE - OFFICERS USE OF FORCE - SUBJECTS \\\n", "State \n", "California \n", " All State Agencies 0 0 \n", " Individual Agency 0 0 \n", "North Carolina \n", " All State Agencies 0 0 \n", " Individual Agency 1 1 \n", "New York \n", "\n", " USE OF FORCE - SUBJECTS/OFFICERS VEHICLE PURSUITS \n", "State \n", "California \n", " All State Agencies 0 0 \n", " Individual Agency 1 2 \n", "North Carolina \n", " All State Agencies 0 0 \n", " Individual Agency 0 0 \n", "New York \n", "\n", "[7 rows x 47 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find number of datasets from each state broken down by table type\n", "opd.datasets.summary_by_state(by=\"table\").head(7)" ] }, { "cell_type": "code", "execution_count": 9, "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", "
TotalDefinition
TableType
STOPS-RELATED
Single Table
STOPS37Contains data on both pedestrian and traffic s...
Multi-Table
TRAFFIC STOPS71Traffic stops are stops by police of motor veh...
.........
POINTING WEAPON2Instances of officers pointing a weapon (firea...
LAWSUITS2Lawsuits against a police department
INCIDENTS - SUBJECTS1Incidents data may be split into several table...
INCIDENTS - INCIDENTS1Incidents data may be split into several table...
DISCIPLINARY RECORDS1Disciplinary records of officers
\n", "

64 rows × 2 columns

\n", "
" ], "text/plain": [ " Total Definition\n", "TableType \n", "STOPS-RELATED \n", " Single Table \n", " STOPS 37 Contains data on both pedestrian and traffic s...\n", " Multi-Table \n", " TRAFFIC STOPS 71 Traffic stops are stops by police of motor veh...\n", "... ... ...\n", "POINTING WEAPON 2 Instances of officers pointing a weapon (firea...\n", "LAWSUITS 2 Lawsuits against a police department\n", "INCIDENTS - SUBJECTS 1 Incidents data may be split into several table...\n", "INCIDENTS - INCIDENTS 1 Incidents data may be split into several table...\n", "DISCIPLINARY RECORDS 1 Disciplinary records of officers\n", "\n", "[64 rows x 2 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find number of datasets for each type of table\n", "opd.datasets.summary_by_table_type()" ] }, { "cell_type": "code", "execution_count": 10, "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", "
TotalN/AMULTI-YEAR2024202320222021202020192018...201120102009200820072006200520042003Definition
TableType
STOPS-RELATED...
Single Table...
STOPS370340333222...000000000Contains data on both pedestrian and traffic s...
Multi-Table...
TRAFFIC STOPS710670355666...111000000Traffic stops are stops by police of motor veh...
\n", "

5 rows × 26 columns

\n", "
" ], "text/plain": [ " Total N/A MULTI-YEAR 2024 2023 2022 2021 2020 2019 2018 \\\n", "TableType \n", "STOPS-RELATED \n", " Single Table \n", " STOPS 37 0 34 0 3 3 3 2 2 2 \n", " Multi-Table \n", " TRAFFIC STOPS 71 0 67 0 3 5 5 6 6 6 \n", "\n", " ... 2011 2010 2009 2008 2007 2006 2005 2004 2003 \\\n", "TableType ... \n", "STOPS-RELATED ... \n", " Single Table ... \n", " STOPS ... 0 0 0 0 0 0 0 0 0 \n", " Multi-Table ... \n", " TRAFFIC STOPS ... 1 1 1 0 0 0 0 0 0 \n", "\n", " Definition \n", "TableType \n", "STOPS-RELATED \n", " Single Table \n", " STOPS Contains data on both pedestrian and traffic s... \n", " Multi-Table \n", " TRAFFIC STOPS Traffic stops are stops by police of motor veh... \n", "\n", "[5 rows x 26 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Find number of datasets for each type of table broken down by year\n", "opd.datasets.summary_by_table_type(by_year=True).head()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# All returned summary tables are pandas DataFrames so they can be easily exported to CSV files\n", "# Find number of datasets for each type of table broken down by year\n", "opd.datasets.summary_by_table_type(by_year=True).to_csv(\"table_summary_by_year.csv\")" ] } ], "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 }