%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
calls = pd.read_csv("911.csv")
calls.timeStamp = pd.to_datetime(calls.timeStamp)
t = calls.title.str.extract("(\w*)([:\s])(\s\w*[\s-]?[/]?\w*/?\w*)",expand = False)
calls["call_type"] = t[0]
calls["call_detail"] = t[2]
calls["Month"] = calls.timeStamp.dt.month
calls["Year"] = calls.timeStamp.dt.year
calls["Hour"] = calls.timeStamp.dt.hour
calls["weekday"] = calls.timeStamp.dt.weekday_name
calls["hour_type"] = np.where(calls.Hour < 5,"MidNight",
np.where((calls.Hour >=5)& (calls.Hour <12),"Morning",
np.where((calls.Hour >=12) &(calls.Hour <17),"AfterNoon","Evening")))
calls.head()
calls.call_type.value_counts().plot(kind = "pie", colors =["salmon","lightsalmon","darksalmon"],
autopct = "%.2f", figsize = (8,8),
explode = [0.01,0.01,0.01])
calls[calls.call_type == "EMS"].call_detail.value_counts().plot(kind = "bar", figsize = (15,8), color = "salmon")
plt.xlabel("Cause Of Incident", size =15)
plt.ylabel("Number of Calls", size = 15)
plt.title("Emergency Calls", size = 15)
calls[calls.call_type == "EMS"].hour_type.value_counts().plot(figsize = (10,9),kind = "pie",autopct = "%.2f", colors = ["tan", "wheat","darksalmon","lightsalmon"],
explode = [0.2,0,0,0])
plt.ylabel("")
plt.title("Emergnecy Calls vs Time of Day", size = 15)
calls[calls.call_type=="EMS"].Hour.value_counts().sort_index().plot(kind = "bar", figsize = (10,4), color = "wheat")
plt.xlabel("Hour", size = 15)
plt.ylabel("Number of Emergency Calls", size =15)
plt.title("Emergency Calls by Hour", size = 15)
calls[calls.call_type == "EMS"].Month.value_counts().sort_index().plot(kind = "line", color = "darksalmon", figsize =(10,4))
plt.xticks(range(1,13,1));
plt.title("Emergency Calls by Month", size =15);
plt.xlabel("Month", size = 15);
plt.ylabel("Number of Calls", size = 15);
calls[calls.call_type == "EMS"].weekday.value_counts().plot(kind = "line", color = "maroon", figsize = (10,4));
plt.title("Emergency Calls vs Week Day", size =15);
plt.xlabel("Week Day", size =15);
plt.ylabel("Number Of Calls", size = 15);
calls[calls.call_type =="EMS"].groupby("twp").count().e.sort_values(ascending = False).plot(kind = "bar", figsize= (15,4), color = "lightsalmon");
plt.title("Emergency Calls vs Town", size = 15)
plt.xlabel("Town", size = 15);
plt.ylabel("Number of Calls", size = 15);
calls[calls.call_type == "Traffic"].call_detail.value_counts().plot(kind = "bar", figsize = (15,8), color = "salmon", rot = 50)
plt.xlabel("Cause Of Incident", size =15);
plt.ylabel("Number of Calls", size = 15);
plt.title("Traffic Calls", size = 15);
calls[calls.call_type == "Traffic"].hour_type.value_counts().plot(figsize = (10,9),kind = "pie",autopct = "%.2f", colors = ["tan", "wheat","darksalmon","lightsalmon"],
explode = [0.2,0,0,0])
plt.title("Traffic Calls vs Time of Day", size = 15)
plt.ylabel("")
calls[calls.call_type=="Traffic"].Hour.value_counts().sort_index().plot(kind = "bar", figsize = (10,4), color = "wheat")
plt.xlabel("Hour", size = 15);
plt.ylabel("Number of Emergency Calls", size = 15);
plt.title("Traffic Calls by Hour", size = 15);
calls[calls.call_type == "Traffic"].Month.value_counts().sort_index().plot(kind = "line", color = "darksalmon", figsize =(10,4))
plt.xticks(range(1,13,1));
plt.title("Traffic Calls by Month", size = 15);
plt.xlabel("Month", size = 15);
plt.ylabel("Number of Calls", size = 15);
calls[calls.call_type == "Traffic"].weekday.value_counts().plot(kind = "line", color = "maroon", figsize = (10,4));
plt.title("Traffic Calls vs Week Day", size = 15);
plt.xlabel("Week Day", size = 15);
plt.ylabel("Number Of Calls", size = 15);
calls[calls.call_type =="Traffic"].groupby("twp").count().e.sort_values(ascending= False).plot(kind = "bar", figsize= (15,4), color = "tan");
plt.title("Traffic Calls vs Town", size = 15);
plt.xlabel("Town", size = 15);
plt.ylabel("Number of Calls", size = 15);
calls[calls.call_type == "Fire"].call_detail.value_counts().plot(kind = "bar", figsize = (15,8), color = "salmon")
plt.xlabel("Cause Of Incident", size =15);
plt.ylabel("Number of Calls", size = 15);
plt.title("Fire Calls", size = 15);
calls[calls.call_type == "Fire"].hour_type.value_counts().plot(figsize = (10,9),kind = "pie",autopct = "%.2f", colors = ["tan", "wheat","darksalmon","lightsalmon"],
explode = [0.2,0,0,0])
plt.title("Fire Calls vs Time of Day", size = 15)
plt.ylabel("")
calls[calls.call_type=="Fire"].Hour.value_counts().sort_index().plot(kind = "bar", figsize = (10,4), color = "wheat")
plt.xlabel("Hour", size = 15);
plt.ylabel("Number of Emergency Calls", size = 15);
plt.title("Fire Calls by Hour", size = 15);
calls[calls.call_type == "Fire"].Month.value_counts().sort_index().plot(kind = "line", color = "darksalmon", figsize =(10,4))
plt.xticks(range(1,13,1));
plt.title("Fire Calls by Month", size = 15);
plt.xlabel("Month", size = 15);
plt.ylabel("Number of Calls", size = 15);
calls[calls.call_type == "Fire"].weekday.value_counts().plot(kind = "line", color = "maroon", figsize = (10,4));
plt.title("Fire Calls vs Week Day", size = 15);
plt.xlabel("Week Day", size = 15);
plt.ylabel("Number Of Calls", size = 15);
calls[calls.call_type =="Fire"].groupby("twp").count().e.sort_values(ascending= False).plot(kind = "bar", figsize= (15,4), color = "tan");
plt.title("Fire Calls vs Town", size = 15);
plt.xlabel("Town", size = 15);
plt.ylabel("Number of Calls", size = 15);
calls.groupby("Hour").call_type.value_counts().unstack().plot(kind = "line",figsize = (16,6))
plt.title("Calls by Hour",size = 15)
plt.ylabel("Number of incidents",size = 15)
plt.xlabel("Hours", size = 15)
plt.xticks(range(0,24,1));
calls.groupby("Month").call_type.value_counts().unstack().plot(kind = "line",figsize = (16,6))
plt.title("Calls by Month",size = 15)
plt.ylabel("Number of incidents",size = 15)
plt.xlabel("Month", size = 15)
plt.xticks(range(0,13,1));
calls.groupby("weekday").call_type.value_counts().unstack().plot(kind = "line",figsize = (16,6))
plt.title("Calls by Weekday",size = 15)
plt.ylabel("Number of incidents",size = 15)
plt.xlabel("Weekday", size = 15)
# plt.xticks(range(0,24,1));
calls.groupby("twp").call_type.value_counts().unstack().sum(1).sort_values(ascending = False).plot(kind = "bar",figsize = (18,8))
# pct_town_details = town_details.div(town_details.sum(0),1)
# pct_town_details.sort_values("EMS",ascending = False).plot(kind = "bar",rot = 90,figsize = (18,8),color = ["red","orange","green"])
plt.xlabel("Town", size = 15);
plt.title("Total Number of Calls by Town", size = 15)
plt.ylabel("Number of Calls", size = 15)
town_details = calls.groupby("twp").call_type.value_counts().unstack()
pct_town_details = town_details.div(town_details.sum(1),0)
pct_town_details.plot(kind = "kde",rot = 90,figsize = (18,8),color = ["red","orange","green"])
# plt.xlabel("Town", size = 15);
# plt.title("Proportion of Call type by Town", size = 15)
# plt.ylabel("Number of Calls", size = 15)