"""
# EDA Project
"""
# importing libraries
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")

# importing the dataset
file_path = "../input/us-accidents/US_Accidents_Dec20_updated.csv"

df = pd.read_csv(file_path)

df.dtypes
df.shape
df.info()
# MISSING VALUES
df.isna().sum()
# missing values column
features_with_na=[features for features in df.columns if df[features].isnull().sum()>1]
features_with_na
for feature in features_with_na:
    print(feature, np.round(df[feature].isnull().mean(), 8),  ' % missing values')
"""
## What are the top cities/states in accidents
"""
len(df['State'].unique())
df['State'].unique()
len(df['City'].unique())
# Taking state in consideration as it has no null values
state_count=df.groupby('State').count().reset_index()
state_count.head(3)

state_count_sort=state_count.sort_values(by = "ID",ascending=False)
state_count_sort.head(3)
a4_dims = (11.7, 20)
fig, ax = plt.subplots(figsize=a4_dims)
sns.barplot(y="State", x="ID",data=state_count_sort)
len(df['City'].unique())
# top city where state is CA
top_city_inca=df.groupby('State').get_group('CA').groupby('City').count().reset_index().sort_values(by = "ID",ascending=False)
top_city_inca=top_city_inca.head(10)
a4_dims = (10, 12)
fig, ax = plt.subplots(figsize=a4_dims)
sns.barplot(y="City", x="ID",data=top_city_inca)
"""
Top state in road accident is CA in which LA city is top city
"""
"""
## Is accidents more in colder areas or warmer areas
"""
# dropping missing values row
df=df.dropna()
df.info()
df['Temperature(F)'].mean()
a4_dims = (10,4)
fig, ax = plt.subplots(figsize=a4_dims)
sns.histplot(df['Temperature(F)'])
plt.boxplot(df['Temperature(F)'])
# state wise temperature distribution
temp_state=df.groupby('State')['Temperature(F)'].mean().reset_index().sort_values(by = "Temperature(F)",ascending=False)
temp_state.head()
temp_state.mean()
temp_state.loc[temp_state['Temperature(F)'] <= 54.5, 'State_condition'] = 'Cold' 
temp_state.loc[temp_state['Temperature(F)'] > 54.5, 'State_condition'] = 'Hot' 
temp_state.head()
state_count_sort=state_count_sort.iloc[:,[0,1]]
state_count_sort.head(5)
final_table=pd.merge(temp_state,state_count_sort,on='State')
final_table=final_table.sort_values(by = "ID",ascending=False)
final_table.head(15)
sns.relplot(x='ID',y='Temperature(F)',hue='State_condition',data=final_table)
"""
From the top 15 accident prone state 9 of them are in hot area while 6 of them are in cool area. 
"""
"""
## At what time the accidents are more
"""
df['Start_Time']
times = pd.to_datetime(df.Start_Time)
times=df.groupby([times.dt.hour]).count().sort_values(by = "ID",ascending=False).iloc[:,[0]]
times=times.reset_index()
sns.barplot(x='Start_Time',y='ID',data=times)
plt.xticks([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23])
plt.show()
"""
From the plot it seems that 12:00 to 18:00 timestamp has high chance of accident.
"""
"""
## In which month accidents are more
"""
times = pd.to_datetime(df.Start_Time)
times=df.groupby([times.dt.month]).count().sort_values(by = "ID",ascending=False).iloc[:,[0]]
times=times.reset_index()
times
sns.barplot(x='Start_Time',y='ID',data=times)
plt.xticks([1,2,3,4,5,6,7,8,9,10,11,12])
plt.show()
"""
From the plot it seems that september to december months have high chance of accident.
"""
"""
## What is trend of accidents over years
"""
times = pd.to_datetime(df.Start_Time)
times=df.groupby([times.dt.year]).count().sort_values(by = "ID",ascending=False).iloc[:,[0]]
times=times.reset_index()
times
a4_dims = (10, 5)
fig, ax = plt.subplots(figsize=a4_dims)
x=times.Start_Time
y=times.ID
plt.plot(x, y, color='Red')
plt.xticks([2016,2017,2018,2019,2020])
plt.show()
"""
## How is the accidents located in the maps
"""
df.info()
import geopandas as gpd
from geopandas import GeoDataFrame
from shapely.geometry import Point,Polygon
import descartes
loc_map=df.groupby(['Start_Lat','Start_Lng']).count().reset_index().sort_values(by = "ID",ascending=False).iloc[:,0:3]
loc_map=loc_map.head(20)
geometry = [Point(xy) for xy in zip(loc_map['Start_Lng'], loc_map['Start_Lat'])]
gdf = GeoDataFrame(loc_map, geometry=geometry)   
#this is a simple map that goes with geopandas
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf.plot(ax=world.plot(figsize=(10, 6)), marker='o', color='red', markersize=15);
import matplotlib.pyplot as plt
plt.scatter(x=loc_map['Start_Lng'], y=loc_map['Start_Lat'])
plt.show()