Skip to content

6.Top 10 Incidents

Ahmed Shahriar Sakib edited this page Dec 30, 2021 · 1 revision

Top Incidents by Frequency

Script - pulse_point_df.title.value_counts().head(10).reset_index().rename(columns={'title':'total','index':'title'})

  • 'Medical Emergency',
  • 'Traffic Collision',
  • 'Fire Alarm',
  • 'Alarm',
  • 'Public Service',
  • 'Refuse/Garbage Fire',
  • 'Structure Fire',
  • 'Mutual Aid',
  • 'Lift Assist',
  • 'Fire'

Group By Date & Time

Script -

pulse_point_top_10_df.groupby(["title","day_name", "time_of_the_day"],
                                     as_index=False).count()[['title','day_name', 
                                                              'time_of_the_day',
                                     'agency']].reset_index(drop=True).rename(columns={'date_of_incident':'date',
                                                                                             'agency':'incident_count'})

Utility Plot Function

# plotly categorical barplot
def plot_top_incident_by_time(title):
  fig = px.bar(top_10_time_df[top_10_time_df.title.str.strip()==title], 
        x="day_name", 
        y="incident_count", 
        color="time_of_the_day", 
        barmode="group",
        labels={'day_name':'Day', 
                'incident_count': 'Incident Count',
                'time_of_the_day': ''},
        title=f"{title} by Time of The Day",
        ).for_each_trace(lambda t: t.update(name=t.name.replace("=","")))
  # remove '=' sign from color
  # https://github.com/plotly/plotly_express/issues/36
  fig.show()


# seaborn alternative

# def plot_top_incident_by_time(title):
#   g=sns.catplot(data=top_10_time_df[top_10_time_df.title.str.strip()==title], 
#                 x="time_of_the_day",
#                 y='incident_count', 
#                 col='day_name',
#                 kind='bar', 
#                 height=6,
#                 col_wrap=4,)

#   for ax in g.axes.flatten():
#       ax.tick_params(labelbottom=True)

#   g.set_ylabels('Incident count')
#   g.set_axis_labels('Time of the Day')
#   g.set_titles("{col_name}")
#   # g.despine(left=True)
#   # plt.suptitle('Incident By Time')
#   plt.show()

Medical Emergency

Medical Emergency

Traffic Collision

Traffic Collision

Fire Alarm

Fire Alarm

Alarm

Alarm

Public Service

Public Service

Refuse/Garbage Fire

Refuse Garbage Fire

Structure Fire

Structure Fire

Mutual Aid

Mutual Aid

Lift Assist

Lift Assist

Fire

Fire