<jupyter_start><jupyter_text>Importing the Python Libraries and the Dataset<jupyter_code># Importing the Python libraries to be used in this project;
# 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Creating a DataFrame from the Kenya Accidents Dataset;
# 
accidents = pd.read_csv('/content/kenya-accidents-database - kenya-accidents-database.csv', delimiter = ',')
accidents<jupyter_output><empty_output><jupyter_text>Data Understanding<jupyter_code># Previewing the first 5 rows of the DataFrame;
# 
accidents.head(5)
# Checking the shape (that is, the number of rows and columns) of the DataFrame;
# 
print('shape of the dataset: ', accidents.shape)
print('dimensions of the dataset: ', accidents.ndim)
print('size of the dataset: ', accidents.size)
# Accessing information about our DataFrame;
# 
accidents.info()
# Exploring the descriptive statistics of the numerical values in the dataset;
# 
accidents.describe().T
# Checking the distribution of the data;
# 
accidents.kurt()
# Plotting the bell-shaped curve;
# 
#plt.style.use('NEW_AGE')
#plt.figure(figsize = (6, 6))
#plt.plot(x, y, color = 'black',
#         linestyle = 'dashed')
  
#plt.scatter(x, y, marker = 'o',
#            s = 25, color = 'red')

#plt.fill_between(x_fill, y_fill, 0,
#                 alpha = 0.2, color = 'blue')
#plt.show()<jupyter_output><empty_output><jupyter_text>Data Preparation  Exploring the relevant columns in the DataFrame ;<jupyter_code># Printing out the column names;
# 
accidents.columns
# The time the accident occurred, 'TIME 24 HOURS';
# 
accidents['TIME 24 HOURS'].unique()
# The most frequently recorded time;
# 
accidents['TIME 24 HOURS'].mode()
# The time the accident occurred 'NEW_TIME';
# 
accidents['NEW_TIME'].unique()
# The most frequently recorded time;
# 
accidents['NEW_TIME'].mode()
accidents[accidents['TIME 24 HOURS']=='NAROK']
# The county the accident occurred, 'COUNTY';
# 
accidents['COUNTY'].unique()
# The gender of the victims, 'GENDER';
# 
accidents['GENDER'].unique()
# The age of the victims, 'AGE';
# 
accidents['AGE'].unique()
accidents[accidents['AGE'] == '46,47,34']
# The cause code of the accident, 'CAUSE CODE';
# 
accidents['CAUSE CODE'].unique()
accidents[accidents['CAUSE CODE'] == 'nan']
# The number of victims, 'NO.';
# 
accidents['NO.'].unique()
# The road the accident occurred, 'ROAD';
# 
accidents['ROAD'].unique()
accidents[accidents['ROAD'] == 'nan']
# The date the accident occurred, 'Date DD/MM/YYYY';
# 
accidents['Date DD/MM/YYYY'].unique()
# Renaming the column 'Date DD/MM/YYYY' to 'DATE';
# 
accidents.rename(columns={"Date DD/MM/YYYY": "DATE"}, inplace = True)
accidents.head(1)<jupyter_output><empty_output><jupyter_text>Completeness<jupyter_code># Checking the dataset for null values;
# 
missing = accidents.isna().sum()
missing = missing[missing != 0]
print('Number of columns with missing values: ', len(missing))

missing.sort_values(ascending = False)<jupyter_output>Number of columns with missing values:  10<jupyter_text>Uniformity<jupyter_code># We rename the columns names into a uniform convention;
# Step 1: We will remove any white spaces;
# Step 2: We will also convert all the column names into lowercase;
# 
accidents.columns=accidents.columns.str.strip().str.lower()
accidents.columns<jupyter_output><empty_output><jupyter_text>Consistency<jupyter_code># We check for any duplicated rows;
# 
print('number of duplicated rows: ', accidents.duplicated().sum()) # There are no duplicated rows;<jupyter_output>number of duplicated rows:  0<jupyter_text>Validity<jupyter_code># We will then drop the columns that will not be needed in the analysis;
# 
accidents.drop(['time 24 hours', 'base/sub base', 'place', 'brief accident details', 'new_age', 'victim'], axis = 1, inplace = True)

# Confirming whether the columns were dropped;
# 
accidents.columns<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code># Previewing the first 5 rows of the DataFrame;
# 
accidents.head(5)
# Checking the column, 'road', for unique values;
# 
accidents['road'].describe()
# Checking the column, 'road', for unique values;
# 
print('Number of roads:', accidents['road'].nunique())
print('\n\n')

accidents['road'].unique()
# Cleaning the column, 'road';
# 
accidents.replace({'road' : {'MOMBASA-NAIROBI' : 'NAIROBI MOMBASA',
                             'MOMBASA NAIROBI' : 'NAIROBI MOMBASA',
                             'NAIROBI - MOMBASA' : 'NAIROBI MOMBASA',
                             'NAIROBI-MOMBASA' : 'NAIROBI MOMBASA',
                             'NAIROBI MOMBASA ROAD' : 'NAIROBI MOMBASA',
                             'NAIROBI - MOMBASA' : 'NAIROBI MOMBASA',
                             'NAIRBI MOMBASA' : 'NAIROBI MOMBASA',
                             'NAIROBI MSA ROAD' : 'NAIROBI MOMBASA',
                             'NAIROBI- MOMBASA' : 'NAIROBI MOMBASA',
                             'NAIROBI -MOMBASA' : 'NAIROBI MOMBASA',
                             'MOMBASA- NAIROBI' : 'NAIROBI MOMBASA',
                             'MSA-NRB' : 'NAIROBI MOMBASA'
                             }}, inplace=True)
accidents.head(5)
# Cleaning the column, 'cause code';
# 
# We will create a new column, 'cause';
# We will impute;
#
print('number of nan values in the column cause code:' ,accidents['cause code'].isnull().sum())

# Imputing the null values using the forward fill method;
# 
accidents['cause code'].fillna(method= 'ffill',inplace= True)
print('\n\n')

cause=[]
for i in accidents['cause code']:
    if i>=1 and i<=30:
        cause.append('Driver')
    elif i>=31 and i<=58:
        cause.append('Cyclist')
    elif i>=59 and i<=68:
        cause.append('Pedestrians')
    elif i>=69 and i<=73:
        cause.append('Passengers')
    elif i>=74 and i<=75:
        cause.append('Animals')
    elif i>=76 and i<=77:
        cause.append('Obstructions')
    elif i>=78 and i<=89:
        cause.append('Vehicle Defects')
    elif i>=90 and i<=93:
        cause.append('Road Deffects')
    elif i>=94 and i<=96:
        cause.append('Weather')
    elif i >=97 and i<=98:
        cause.append('Other Causes')
    elif i>=99:
        cause.append('No account')
;
accidents['cause'] = cause
# Cleaning the column, 'age';
# 
# Create a new column for age;
# 
accidents['Cleaned_Age'] = accidents['age']

# Create a new column for the cleaned AGE data;
# 
accidents.replace({'Cleaned_Age' : { '6 MONTHS' : 0.5, '5 MONTHS': 0.4,'3 MONTHS': 0.25,'20 & 25 YRS' : 22.5,'23 & 27': 25,'39,30,40,33, & 30':34,
'29 & 32': 31.5,'22,65 & 9 MONTHS': 26.4,'30 & 33' : 31.5,'46,47,34': 42.3,'21& 28':24.5,'20 & 25 YRS': 22.5,
 '34 AND 24': 27,'45&52': 48.5,'22 YRS' : 22,'28, 31 YRS': 29.5,'25&28': 26.5,'52 &17': 34.5,'25&19': 22,'21 & 21': 21,
 '42,24,32': 32,'40,38,45': 41,'4MTS': 0.3}}, inplace = True)

# Checking the unique values to confirm if the above were deleted;
# 
accidents['Cleaned_Age'].unique()

# Getting the mean of the values;
# 
Mean_Age = pd.to_numeric(accidents['Cleaned_Age'], errors = 'coerce').mean()

accidents.fillna({
    'age' : Mean_Age
    
},inplace = True)

# Checking the unique values again;
# 
accidents['Cleaned_Age'].unique()

# Replacing the str values with the mean;
#
accidents.replace({'Cleaned_Age': { 'J' : Mean_Age, 'A & J' : Mean_Age,'A&J': Mean_Age,'M':Mean_Age,
'A': Mean_Age}}, inplace = True)

# Check the data set if column created;
# 
accidents.head(5)
# Cleaning the column, 'county';
# 
print('number of counties',accidents['county'].nunique())
print('\n\n')
accidents['county'].unique()
# Replacing;
# 
accidents.replace({'county' : {'HOMA BAY': 'HOMABAY','HOMA-BAY': 'HOMABAY','TIGANIA': 'MERU','KILFI': 'KILIFI','MWINGI':'KITUI','NYAHURURU': 'LAIKIPIA','TAITA-TAVETA': 'TAIT<NAME>','KERCHO': 'KERICHO',"MURANG'A": 'MURANGA'}}, inplace= True)
accidents['county'].describe()
# Combining the 'date' and 'new_time' columns and converting the values to datetime format;
# 
accidents['dt'] = pd.to_datetime(accidents['date'] + ' ' + accidents['new_time'])
accidents.head(1)
# Confirming the change has taken place;
# 
accidents.dtypes 
# Creating a new column month and populating it;
# 
month=[]
for i in accidents['dt'].dt.month:
    if i==1:
        month.append('Jan')
    elif i==2:
        month.append('Feb')
    elif i==3:
        month.append('Mar')
    elif i==4:
        month.append('Apr')
    elif i==5:
        month.append('May')
    elif i==6:
        month.append('Jun')
    elif i==7:
        month.append('Jul')
    elif i==8:
        month.append('Aug')
    elif i==9:
        month.append('Sep')
    elif i ==10:
        month.append('Oct')
    elif i==11:
        month.append('Nov')
    elif i==12:
        month.append('Dec')
    else:
        month.append('Null')
;
accidents['month'] = month
# Creating a new column time;
accidents['time'] = [d.time() for d in accidents['dt']]
accidents.columns
# Dropping more columns that will not be needed in the analysis;
# 
accidents.drop(['new_time', 'gender', 'date', 'cause code', 'age'], axis = 1 , inplace = True)
accidents.columns<jupyter_output><empty_output><jupyter_text>Analysis  1. On which roads are there the most victims? Which areas or counties do these accidents occur in?<jupyter_code>accidentsroads = pd.pivot_table(accidents, values = ['no.'], index = ['road'], aggfunc = np.sum)
accidentsroads.sort_values(by = 'no.', ascending = False).head(3)
accidentscounties = pd.pivot_table(accidents, values = ['no.'], index=['county'], aggfunc = np.sum)
accidentscounties.sort_values(by = 'no.',ascending = False).head()<jupyter_output><empty_output><jupyter_text>2. What is the most common cause of road accidents?<jupyter_code>print('Number of causes of accidents: ', accidents['cause'].nunique())
print('\n\n')
accidentcause = accidents.groupby('cause')['no.'].agg('sum').sort_values(ascending = False)
accidentcause
plt.figure(figsize = (8,8))
plt.title('Distribution of Causes of Accidents', fontdict = {'fontsize':12}, color = 'blue')
labels = ['Driver','Other Causes','Pedestrians','Cyclist','Vehicle Defects','Passengers','Road Deffects','Obstructions','No account']
accidentcause.plot(kind='bar')
plt.xlabel('Causes')
plt.xticks(rotation=30)
plt.ylabel('Number of victims')
plt.show()<jupyter_output><empty_output><jupyter_text>3. What time do most fatalities occur due to accidents?<jupyter_code>accidentsno = pd.pivot_table(accidents, values = ['no.'], index = ['time'], aggfunc = np.sum)
accidentsno.sort_values(by = 'no.',ascending = False).head(5)<jupyter_output><empty_output><jupyter_text>4. What time do most accidents happen?<jupyter_code>accidents['time'].value_counts()
plt.figure(figsize=(8,8))
accidents.plot(x='time', y='no.', kind='line')
plt.show()
accidents[accidents['no.']==18]
# from the output above, we can see that at  2:30 am we had an accident that had 18 victims<jupyter_output><empty_output><jupyter_text>5. Which months of the year do most accidents occur?<jupyter_code>months = accidents['dt'].dt.month.value_counts()
print('Number of unique Months: ', len(months))
print('\n\n')
months
plt.figure(figsize = (8,8))
plt.title('Distribution of Accidents by Months', fontdict = {'fontsize':12}, color = 'blue')
plt.xlabel('Month')
accidents['month'].value_counts().plot(kind = 'bar')
plt.ylabel('Count of Accident Victims')
plt.show()<jupyter_output><empty_output><jupyter_text>6. Which age had the most victims in accidents?<jupyter_code>accidentsage = pd.pivot_table(accidents,values = ['no.'], index = ['Cleaned_Age'], aggfunc = np.sum)
accidentsage.sort_values(by = 'no.',ascending = False).head(5)<jupyter_output><empty_output><jupyter_text>7. Which gender had the most victims in accidents?<jupyter_code>gender = accidents[['male_count', 'female_count', 'unknown_gender_count']]
gender.sum()
plt.figure(figsize = (6,6))
plt.title('Distribution of Victims by Gender', fontdict = {'fontsize':12}, color = 'blue')
labels = ['male_count', 'female_count', 'unknown_gender_count']
explode = 0.1, 0.2, 0
plt.pie(gender.sum(), labels = labels, explode = explode, autopct = '%1.3f%%')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15884834946636883, 'fraction_numerical': 0.023496318358567057, 'mean_word_length': 4.379617267467735, 'pattern_counts': {'":': 2, '<': 70, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '328348', 'license': 'MIT', 'n_tokens_mistral': 4485, 'n_tokens_neox': 4187, 'n_words': 1053, 'path': 'Moringa_Data_Science_Prep_W5_Data_Science_Prep_Project_2021_05_Team_Jabari_B_Python_Notebook.ipynb', 'repo_name': 'GMaina716/Kenyan-Accident-Analysis', 'size': '12089'}