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

# ### Question: When during the day is most dangerous?
#
# Let's see how accidents are splitted based on the place of the event and see where we can feel to be the safest.
#
# First step before any data analysis is to import required libraries and data. Any information required to understand columns is available here: https://www.kaggle.com/ahmedlahlou/accidents-in-france-from-2005-to-2016.

# +
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline
import seaborn as sns

dataset = pd.read_csv('data/caracteristics.csv', encoding='latin1')
dataset.head()
# -

# Let's check the 'lum' column and change the values there.
# * 1 - Full day
# * 2 - Twilight or dawn
# * 3 - Night without public lighting
# * 4 - Night with public lighting not lit
# * 5 - Night with public lighting on
#
# But first check if there are a missing values in column that we want to work on.

dataset.columns[dataset.isna().sum() != 0]

# Now, let's change the 'lum' column to be more understandable by humans.

dataset['lum'] = dataset['lum'].astype(str) 
dataset['lum'] = dataset['lum'].replace({'2': 'Twilight or dawn', '1': 'Full day', '3': 'Night no public lighting', '4': 'Night lighting not lit', '5': 'Night with public lighting on'})

plt.clf()
ax = sns.countplot(y = 'lum', data=dataset)
ax.set_title('Number of accidents based on the lightning conditions')
ax.set_xlabel('Number of accidents')
ax.set_ylabel('Light conditions')
plt.show()

# Now let's see summarization when during all that years there was the highest number of accidents. First let's check what type is in 'hrmn' column which represents combined hour and minutes.

dataset.dtypes

# Ceate new column 'hr' where we'll put only hour of accident in 24 hour format.

dataset['hr'] = dataset['hrmn']/100
dataset['hr'] = dataset['hr'].astype('int64')
dataset.head()

plt.clf()
plt.figure(figsize=(10,7))
ax = sns.countplot(x='hr', data=dataset, palette="GnBu_d")
ax.set_title('Number of accidents during 24-hours')
ax.set_xlabel('Hour')
ax.set_ylabel('Number of accidents')
plt.show()

# It looks like the most of accidents are happening during the day, with two peaks. First peak is between 8:00 and 9:00 and second between 17:00 and 18:00.

StarcoderdataJupyterScriptsDedupFiltered