# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/matthewmyrick/DataAnalyticsPortfolio/blob/main/US%20Collision/Collision_Data_Analysis.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="WKotcjbX1ow9"
import pandas as pd
import sqlite3 
import matplotlib.pyplot as plt

# + colab={"base_uri": "https://localhost:8080/"} id="6Io_9O6OLeAb" outputId="99abb834-a8ef-4d51-fc1e-f0f28522c243"
from google.colab import drive
drive.mount('/content/drive')
# git hub path [US Collision/Collision_Data_Analysis.ipynb]

# + id="m4kLaUH33Doq"
CSV_PATH = "/content/drive/MyDrive/Data Analytics Portfolio/data/US_Accidents_Dec20_updated.csv"

# + colab={"base_uri": "https://localhost:8080/", "height": 894} id="bWs4PlffK-Z5" outputId="0b53c1f2-f74b-4953-ee62-517c7b6cca16"
# create and edit dateframe
df = pd.read_csv(CSV_PATH)
date_list = []
for date_str in df.Start_Time:
  date_split = date_str.split(" ")
  date_list.append(date_split[0])
df['Date'] = date_list
df 

# + id="HCUpqDAQOoHG" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="0f69af1b-d1e2-4b7e-cfb2-45e0dfe96afc"
# set up histogram data for amount of accident reports per state
state_lists = df.State.unique()
df_state_count = df.groupby(['State']).agg({
    "ID":['count']
    })
hist_val = df_state_count[('ID', 'count')].to_list()
df_state_count

# + colab={"base_uri": "https://localhost:8080/", "height": 460} id="9n57OAnUPJZx" outputId="9f7870f4-9861-4740-fa81-65ab945ac6e6"
# plot histogram 
fig, axs = plt.subplots()
axs.bar(state_lists, hist_val)
plt.rcParams['figure.figsize'] = [25, 8]
plt.show()

# + id="hnlouzEyfXSS" colab={"base_uri": "https://localhost:8080/", "height": 237} outputId="87110dc8-510c-40db-b7d9-401c59dd54f3"
# set up data for severity report)
df_severity_count = df.groupby(['Severity']).agg({
    "ID":['count']
    })
severity_list = df_severity_count.index.to_list()
pie_val = df_severity_count[('ID', 'count')].to_list()
df_severity_count

# + colab={"base_uri": "https://localhost:8080/", "height": 482} id="lhALNVKStz8R" outputId="42f4f884-83b9-4e09-b11f-98205c2e509d"
fig, axs = plt.subplots()
axs.pie(pie_val, labels=severity_list, autopct='%1.1f%%')
plt.title("Number of crashes by Severity")
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 486} id="T0cdHlSuv3Ms" outputId="d522bb76-a0be-4ea7-b8d8-a2440fc06821"
# create time series data
df_timeseries = df.groupby(['Date']).agg({
    "ID":['count']
    })
date_list = df_timeseries.index.to_list()
line_val = df_timeseries[('ID', 'count')].to_list()
df_timeseries

# + colab={"base_uri": "https://localhost:8080/", "height": 510} id="PCSDKR9h04fc" outputId="67a66210-675d-46be-bf75-0546813ccffa"
df_timeseries.plot.line()
plt.title("Number of Reported Accidents per Date period")

# + colab={"base_uri": "https://localhost:8080/", "height": 486} id="1eaivbHXyKgA" outputId="f98459bc-b007-48d4-f366-4f32b8cf354b"
# Get high overview of series data 
df_timeseries

StarcoderdataJupyterScriptsDedupFiltered