Police Crime Statistics with Python (for a neighbourhood)

#!/usr/bin/env python3
import sys
from police_api import PoliceAPI
from police_api.neighbourhoods import Neighbourhood
from police_api.forces import Force

api = PoliceAPI()
force = Force(api, id='hertfordshire')
neighbourhood = force.get_neighbourhood('D04')

import csv
with open('hemel-south.csv', mode='w') as hemel_south_file:
  hemel_south_writer = csv.writer(hemel_south_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
  date_list = api.get_dates()
  hemel_south_writer.writerow(['Date', 'Category', 'Location','Outcome','Longitude', 'Latitude'])
  for n in range(12):
    date_chunk = date_list[n]
    crimlist= api.get_crimes_area(neighbourhood.boundary, date=date_chunk)
    for crime in crimlist:
      hemel_south_writer.writerow([crime.month, str(crime.category).replace("<CrimeCategory>",""), crime.location.name, crime.outcome_status,crime.location.longitude, crime.location.latitude])

import pandas
import plotly.express as px
import sys
df = pandas.read_csv("hemel-south.csv")
df = df.groupby(['Date','Category'])['Category'].count()   .reset_index(name='Count')
fig =  px.line(df, x = 'Date', y = 'Count', title='Crime In Hemel South',  color='Category', hover_name = 'Category' )
fig.show()

Leave a Reply

Your email address will not be published. Required fields are marked *