{ "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": 4, "metadata": {}, "outputs": [], "source": [ "try:\n", " import openpolicedata as opd\n", "except:\n", " import sys\n", " sys.path.append('../openpolicedata')\n", " import openpolicedata as opd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The OpenPoliceData package has 212 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": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OPD has 9 datasets for all agencies in a state\n", "OPD has 120 datasets for 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 {opd.datasets.num_sources(full_states_only=True)} datasets for all agencies in a state\")\n", "print(f\"OPD has {opd.datasets.num_sources()-opd.datasets.num_sources(full_states_only=True)} datasets for 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 Agency23
Texas20
North Carolina
All State Agencies1
Individual Agency17
New York12
Ohio10
Arizona9
\n", "
" ], "text/plain": [ " Total\n", "State \n", "California \n", " All State Agencies 2\n", " Individual Agency 23\n", "Texas 20\n", "North Carolina \n", " All State Agencies 1\n", " Individual Agency 17\n", "New York 12\n", "Ohio 10\n", "Arizona 9" ] }, "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-YEAR2022202120202019201820172016...2014201320122011201020092008200720062005
State
California...
All State Agencies2000122211...0000000000
Individual Agency230180554444...2222100000
Texas200130026665...5211110000
North Carolina...
All State Agencies1010000000...0000000000
Individual Agency172140111111...1111111111
\n", "

7 rows × 21 columns

\n", "
" ], "text/plain": [ " Total N/A MULTI-YEAR 2022 2021 2020 2019 2018 2017 2016 \\\n", "State \n", "California \n", " All State Agencies 2 0 0 0 1 2 2 2 1 1 \n", " Individual Agency 23 0 18 0 5 5 4 4 4 4 \n", "Texas 20 0 13 0 0 2 6 6 6 5 \n", "North Carolina \n", " All State Agencies 1 0 1 0 0 0 0 0 0 0 \n", " Individual Agency 17 2 14 0 1 1 1 1 1 1 \n", "\n", " ... 2014 2013 2012 2011 2010 2009 2008 2007 2006 2005 \n", "State ... \n", "California ... \n", " All State Agencies ... 0 0 0 0 0 0 0 0 0 0 \n", " Individual Agency ... 2 2 2 2 1 0 0 0 0 0 \n", "Texas ... 5 2 1 1 1 1 0 0 0 0 \n", "North Carolina ... \n", " All State Agencies ... 0 0 0 0 0 0 0 0 0 0 \n", " Individual Agency ... 1 1 1 1 1 1 1 1 1 1 \n", "\n", "[7 rows x 21 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 SERVICECITATIONSCOMPLAINTSEMPLOYEEFIELD CONTACTSOFFICER-INVOLVED SHOOTINGSOFFICER-INVOLVED SHOOTINGS - CIVILIANSOFFICER-INVOLVED SHOOTINGS - INCIDENTS...TRAFFIC ARRESTSTRAFFIC CITATIONSTRAFFIC STOPSTRAFFIC WARNINGSUSE OF FORCEUSE OF FORCE - CIVILIANSUSE OF FORCE - CIVILIANS/OFFICERSUSE OF FORCE - INCIDENTSUSE OF FORCE - OFFICERSVEHICLE PURSUITS
State
California...
All State Agencies2000000000...0000000100
Individual Agency23050100011...0070001000
Texas20000000011...1151210000
North Carolina...
All State Agencies1000000000...0010000000
Individual Agency17011021011...0070010010
\n", "

7 rows × 26 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 23 0 5 0 1 \n", "Texas 20 0 0 0 0 \n", "North Carolina \n", " All State Agencies 1 0 0 0 0 \n", " Individual Agency 17 0 1 1 0 \n", "\n", " EMPLOYEE FIELD CONTACTS OFFICER-INVOLVED SHOOTINGS \\\n", "State \n", "California \n", " All State Agencies 0 0 0 \n", " Individual Agency 0 0 0 \n", "Texas 0 0 0 \n", "North Carolina \n", " All State Agencies 0 0 0 \n", " Individual Agency 2 1 0 \n", "\n", " OFFICER-INVOLVED SHOOTINGS - CIVILIANS \\\n", "State \n", "California \n", " All State Agencies 0 \n", " Individual Agency 1 \n", "Texas 1 \n", "North Carolina \n", " All State Agencies 0 \n", " Individual Agency 1 \n", "\n", " OFFICER-INVOLVED SHOOTINGS - INCIDENTS ... \\\n", "State ... \n", "California ... \n", " All State Agencies 0 ... \n", " Individual Agency 1 ... \n", "Texas 1 ... \n", "North Carolina ... \n", " All State Agencies 0 ... \n", " Individual Agency 1 ... \n", "\n", " TRAFFIC ARRESTS TRAFFIC CITATIONS TRAFFIC STOPS \\\n", "State \n", "California \n", " All State Agencies 0 0 0 \n", " Individual Agency 0 0 7 \n", "Texas 1 1 5 \n", "North Carolina \n", " All State Agencies 0 0 1 \n", " Individual Agency 0 0 7 \n", "\n", " TRAFFIC WARNINGS USE OF FORCE USE OF FORCE - CIVILIANS \\\n", "State \n", "California \n", " All State Agencies 0 0 0 \n", " Individual Agency 0 0 0 \n", "Texas 1 2 1 \n", "North Carolina \n", " All State Agencies 0 0 0 \n", " Individual Agency 0 0 1 \n", "\n", " USE OF FORCE - CIVILIANS/OFFICERS \\\n", "State \n", "California \n", " All State Agencies 0 \n", " Individual Agency 1 \n", "Texas 0 \n", "North Carolina \n", " All State Agencies 0 \n", " Individual Agency 0 \n", "\n", " USE OF FORCE - INCIDENTS USE OF FORCE - OFFICERS \\\n", "State \n", "California \n", " All State Agencies 1 0 \n", " Individual Agency 0 0 \n", "Texas 0 0 \n", "North Carolina \n", " All State Agencies 0 0 \n", " Individual Agency 0 1 \n", "\n", " VEHICLE PURSUITS \n", "State \n", "California \n", " All State Agencies 0 \n", " Individual Agency 0 \n", "Texas 0 \n", "North Carolina \n", " All State Agencies 0 \n", " Individual Agency 0 \n", "\n", "[7 rows x 26 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": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Total \\\n", "TableType \n", "STOPS \n", " TRAFFIC STOPS (Only) 66 \n", " STOPS (All) 31 \n", " PEDESTRIAN STOPS (Only) 2 \n", "CALLS FOR SERVICE 22 \n", "USE OF FORCE \n", " Single Table \n", " USE OF FORCE 13 \n", " Multiple Tables \n", " USE OF FORCE - CIVILIANS 3 \n", " USE OF FORCE - INCIDENTS 2 \n", " USE OF FORCE - OFFICERS 2 \n", " USE OF FORCE - CIVILIANS/OFFICERS 1 \n", "OFFICER-INVOLVED SHOOTINGS \n", " Single Table \n", " OFFICER-INVOLVED SHOOTINGS 12 \n", " Multiple Tables \n", " OFFICER-INVOLVED SHOOTINGS - INCIDENTS 4 \n", " OFFICER-INVOLVED SHOOTINGS - OFFICERS 4 \n", " OFFICER-INVOLVED SHOOTINGS - CIVILIANS 4 \n", "COMPLAINTS 9 \n", "EMPLOYEE 9 \n", "ARRESTS \n", " ARRESTS (All) 6 \n", " TRAFFIC ARRESTS (Only) 2 \n", " PEDESTRIAN ARRESTS (Only) 1 \n", "CITATIONS \n", " CITATIONS (All) 5 \n", " TRAFFIC CITATIONS (Only) 4 \n", " PEDESTRIAN CITATIONS (Only) 2 \n", "WARNINGS \n", " TRAFFIC WARNINGS (Only) 3 \n", " PEDESTRIAN WARNINGS (Only) 1 \n", "FIELD CONTACTS 2 \n", "VEHICLE PURSUITS 2 \n", "\n", " Definition \n", "TableType \n", "STOPS \n", " TRAFFIC STOPS (Only) Traffic stops are stops by police of motor veh... \n", " STOPS (All) Contains data on both pedestrian and traffic s... \n", " PEDESTRIAN STOPS (Only) Stops of pedestrians based on 'reasonable susp... \n", "CALLS FOR SERVICE Includes dispatched calls (911 or non-emergenc... \n", "USE OF FORCE \n", " Single Table \n", " USE OF FORCE Documentation of physical force used against c... \n", " Multiple Tables \n", " USE OF FORCE - CIVILIANS Use of force data may be split into several ta... \n", " USE OF FORCE - INCIDENTS Use of force data may be split into several ta... \n", " USE OF FORCE - OFFICERS Use of force data may be split into several ta... \n", " USE OF FORCE - CIVILIANS/OFFICERS Use of force data may be split into several ta... \n", "OFFICER-INVOLVED SHOOTINGS \n", " Single Table \n", " OFFICER-INVOLVED SHOOTINGS Shootings by officers \n", " Multiple Tables \n", " OFFICER-INVOLVED SHOOTINGS - INCIDENTS Officer-involved shootings data may be split i... \n", " OFFICER-INVOLVED SHOOTINGS - OFFICERS Officer-involved shootings data may be split i... \n", " OFFICER-INVOLVED SHOOTINGS - CIVILIANS Officer-involved shootings data may be split i... \n", "COMPLAINTS Complaints of police misconduct made internall... \n", "EMPLOYEE Demographic data of the police workforce \n", "ARRESTS \n", " ARRESTS (All) Seizures or forcible restraints by police \n", " TRAFFIC ARRESTS (Only) Traffic stops leading to an arrest. \n", " PEDESTRIAN ARRESTS (Only) Pedestrian stops leading to an arrest \n", "CITATIONS \n", " CITATIONS (All) Commonly referred to as tickets, citations ind... \n", " TRAFFIC CITATIONS (Only) Traffic stops leading to a citation. \n", " PEDESTRIAN CITATIONS (Only) Pedestrian stops leading to a citation \n", "WARNINGS \n", " TRAFFIC WARNINGS (Only) Traffic stops leading to a warning. \n", " PEDESTRIAN WARNINGS (Only) Pedestrian stops leading to a warning \n", "FIELD CONTACTS Consensual contacts between officers and the c... \n", "VEHICLE PURSUITS Attempts by officers in vehicles to pursue veh... \n" ] } ], "source": [ "# Find number of datasets for each type of table\n", "print(opd.datasets.summary_by_table_type())" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \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-YEAR2022202120202019201820172016...201320122011201020092008200720062005Definition
TableType
STOPS...
TRAFFIC STOPS (Only)660640022222...211110000Traffic stops are stops by police of motor veh...
STOPS (All)310300011100...000000000Contains data on both pedestrian and traffic s...
PEDESTRIAN STOPS (Only)2010000001...110000000Stops of pedestrians based on 'reasonable susp...
CALLS FOR SERVICE220142888776...554211111Includes dispatched calls (911 or non-emergenc...
\n", "

5 rows × 22 columns

\n", "
" ], "text/plain": [ " Total N/A MULTI-YEAR 2022 2021 2020 2019 2018 2017 \\\n", "TableType \n", "STOPS \n", " TRAFFIC STOPS (Only) 66 0 64 0 0 2 2 2 2 \n", " STOPS (All) 31 0 30 0 0 1 1 1 0 \n", " PEDESTRIAN STOPS (Only) 2 0 1 0 0 0 0 0 0 \n", "CALLS FOR SERVICE 22 0 14 2 8 8 8 7 7 \n", "\n", " 2016 ... 2013 2012 2011 2010 2009 2008 2007 2006 \\\n", "TableType ... \n", "STOPS ... \n", " TRAFFIC STOPS (Only) 2 ... 2 1 1 1 1 0 0 0 \n", " STOPS (All) 0 ... 0 0 0 0 0 0 0 0 \n", " PEDESTRIAN STOPS (Only) 1 ... 1 1 0 0 0 0 0 0 \n", "CALLS FOR SERVICE 6 ... 5 5 4 2 1 1 1 1 \n", "\n", " 2005 \\\n", "TableType \n", "STOPS \n", " TRAFFIC STOPS (Only) 0 \n", " STOPS (All) 0 \n", " PEDESTRIAN STOPS (Only) 0 \n", "CALLS FOR SERVICE 1 \n", "\n", " Definition \n", "TableType \n", "STOPS \n", " TRAFFIC STOPS (Only) Traffic stops are stops by police of motor veh... \n", " STOPS (All) Contains data on both pedestrian and traffic s... \n", " PEDESTRIAN STOPS (Only) Stops of pedestrians based on 'reasonable susp... \n", "CALLS FOR SERVICE Includes dispatched calls (911 or non-emergenc... \n", "\n", "[5 rows x 22 columns]" ] }, "execution_count": 9, "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.8.10" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "a73158d29711b2da05ac73de25b71e5d8cae591f14917bba77a9573b5c85a0ce" } } }, "nbformat": 4, "nbformat_minor": 2 }