Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

# Created by https://www.gitignore.io/api/macos,windows,jupyternotebooks,visualstudiocode
# Edit at https://www.gitignore.io/?templates=macos,windows,jupyternotebooks,visualstudiocode

### JupyterNotebooks ###
# gitignore template for Jupyter Notebooks
# website: http://jupyter.org/

.ipynb_checkpoints
*/.ipynb_checkpoints/*

# IPython
profile_default/
ipython_config.py

# Remove previous ipynb_checkpoints
# git rm -r .ipynb_checkpoints/

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

#EC Added - folders to ignore
your-project/images/
your-project/archive/
your-project/working/

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/macos,windows,jupyternotebooks,visualstudiocode
19,080 changes: 19,080 additions & 0 deletions datasets/1.-Transportation/2018_accidents_vehicles_gu_bcn_.csv

Large diffs are not rendered by default.

Binary file not shown.
172 changes: 172 additions & 0 deletions your-project/CSV_to_SQL.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#Libraries\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"#Connection to DB\n",
"from sqlalchemy import create_engine\n",
"driver = 'mysql+pymysql'\n",
"ip = 'barcelona-db.cyxhqbnhiohl.eu-west-3.rds.amazonaws.com'\n",
"username = 'admin'\n",
"password = 'PercyA2019!'\n",
"db = 'project2'\n",
"connection_string = f'{driver}://{username}:{password}@{ip}/{db}'\n",
"# Engine & Query\n",
"engine = create_engine(connection_string)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#Step 1 - Load dataframe\n",
"stg_df = pd.read_csv(\"../datasets/1.-Transportation/2018_accidents_vehicles_gu_bcn_.csv\") "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Codi_expedient object\n",
"Codi_districte int64\n",
"Nom_districte object\n",
"Codi_barri int64\n",
"Nom_barri object\n",
"Codi_carrer int64\n",
"Nom_carrer object\n",
"Num_postal object\n",
"Descripcio_dia_setmana object\n",
"Dia_setmana object\n",
"Descripcio_tipus_dia object\n",
"Any int64\n",
"Mes_any int64\n",
"Nom_mes object\n",
"Dia_mes int64\n",
"Hora_dia int64\n",
"Descripcio_torn object\n",
"Descripcio_causa_vianant object\n",
"Descripcio_tipus_vehicle object\n",
"Descripcio_model object\n",
"Descripcio_marca object\n",
"Descripcio_color object\n",
"Descripcio_carnet object\n",
"Antiguitat_carnet object\n",
"Coordenada_UTM_X float64\n",
"Coordenada_UTM_Y float64\n",
"Longitud float64\n",
"Latitud float64\n",
"dtype: object"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Step 2 - Check Data Types\n",
"stg_df.dtypes"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#Step 3 - Rename Problem Column Name\n",
"stg_df.rename(columns={'Num_postal ':'Num_postal'}, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#Step 4 - Remove whitespace in selected columns\n",
"stg_df['Codi_expedient_clean'] = stg_df.loc[:, 'Codi_expedient'].str.strip()\n",
"stg_df['Nom_carrer_clean'] = stg_df.loc[:, 'Nom_carrer'].str.strip()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Step 5 - Create a 'datetime column'\n",
"stg_df['Date'] = stg_df[['Dia_mes', 'Mes_any', 'Any']].apply(lambda x: str(x.Dia_mes)+ '-' + str(x.Mes_any) + '-' + str(x.Any), axis=1)\n",
"stg_df['Date'] = stg_df.Date.astype('datetime64[ns]')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"#Step 6 - Create 4 new data frames from the raw file\n",
"accident = stg_df[['Codi_expedient_clean','Codi_carrer','Descripcio_causa_vianant']]\n",
"accident_datetime = stg_df[['Codi_expedient_clean','Hora_dia','Dia_mes','Mes_any','Any', 'Date']]\n",
"accident_location = stg_df[['Codi_expedient_clean','Coordenada_UTM_X','Coordenada_UTM_Y','Longitud','Latitud']]\n",
"accident_street = stg_df[['Codi_expedient_clean','Nom_carrer_clean','Nom_barri','Nom_districte']]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"#Step 7 - Push new DFs to SQL\n",
"stg_df.to_sql('stg_data', con=engine, if_exists='replace')\n",
"accident.to_sql('stg_accident', con=engine, if_exists='replace')\n",
"accident_datetime.to_sql('stg_accident_datetime', con=engine, if_exists='replace')\n",
"accident_location.to_sql('stg_accident_location', con=engine, if_exists='replace')\n",
"accident_street.to_sql('stg_street', con=engine, if_exists='replace')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added your-project/ERD (Inc Future Improvements).pptx
Binary file not shown.
Binary file not shown.
Binary file not shown.
56 changes: 43 additions & 13 deletions your-project/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<img src="https://bit.ly/2VnXWr2" alt="Ironhack Logo" width="100"/>

# Title of My Project
*[Your Name]*
# Analysis of Road Traffic Accidents in Barcelona
*Elliott and Sabbah *

*[Your Cohort, Campus & Date]*
*Data Analysis, October 2019 - Barcelona*

## Content
- [Project Description](#project-description)
Expand All @@ -16,28 +16,58 @@


## Project Description
Write a short introduction to your project: 3-5 sentences about the context of your topic and why you chose it.
The local population of Barcelona are well known for taking their annual holidays during the summar month of August. During the month many businesses close - however there is an influx of visitors who are also taking their summer break. The aim of this project is to determine if there is a change in the number Road Traffic Accidents (RTAs) during the month of August.

## Questions & Hypotheses
What are the questions you would like to answer with your analysis? What did you feel were the answers to those questions before answering them with data?
The authors of this project have assumed that the number of accidents willincrease during the month of August, compared to the rest of the year.
The questions to be answered during the analysis portion of this project are:
1. Does the month of year influnce the number of RTAs in Barcelona?
2. Does the time of day influence the number of RTAs?
3. Is there seasonabl variation in the numner fo RTAs?
3. If there a variation in the number of accidents by district within the city.

## Dataset
What dataset (or datasets) did you use? What is the source of your data? Provide links to the data if available and describe the data briefly.
The data for this project was sourced from the Ajuntament of Barcelona Open Data website (accessible in English language). The URL for this raw data CSV file is provided in the Links section below.

## Database
What is the structure of your database? Have you created more than one table and if yes, how are they related to each other? Include a drawing or computer-generated image of the ERD (Entity Relationship Diagram) of your database.
The raw CSV data file was cleaned using Pandas and subsequently populated into tables within a MySQL databse hosted on AWS. The structure and relationships of the tables is demonstrated in the ERD below. Note that not all data in the source file was imported into the database for analysis.

![logo](https://www.dropbox.com/s/ppuwne3jhkv41d6/ERD.JPG?raw=1) "Barcelona Accident Data 2018 - ERD"


You may access the database using the credentials below

endpoint: barcelona-db.cyxhqbnhiohl.eu-west-3.rds.amazonaws.com

username: guest

password: ABCd1234

## Workflow
Outline the workflow you used in your project. What are the steps you went through?
The following steps were performed during the analysis of this project.
1. Locate a suitable raw data file.
2. Import CSV file into Pandas dataframe for initial analysis and review. [One column contained a trailling space which caused an issue]
3. ERD created based on data available and analysis to be performed.
4. Remove whitespace from column names and selected columns (accident identifier and street name).
5. New dataframes created using selected columns from raw data file.
6. Population of aforementiones new dataframes - used cleaned columns as required.
7. New database created on Amazon Web Services (AWS).
8. Respective Pandas dataframes used to populate the tables on the new database.
9. Analysis performed via both SQL quieres and within Pandas to develop our skill sets using eact methodology.

## Organization
How did you organize your work? Did you use any tools like a kanban board?
In order to idenitfy and track all tasks required to be completed, a Trello board was utilised. The link to the Trello board used is found in the Links section below.

Within the GitHub respository there are two folders.

The 'your-project' folder contains a the IPYNB and SQL files used to load the raw data CSV, which also resides here. The ERD is in this folder too.

What does your repository look like? Explain your folder and file structure.
The 'datasets' folders contains a selection of other datasets which were available (but not used) during the discovery phase of this project.

## Links
Include links to your repository, slides and kanban board. Feel free to include any other links associated with your project.

[Repository](https://github.com/)
[Slides](https://slides.com/)
[Trello](https://trello.com/en)
[Repository](https://github.com/tristar82/Project-Week-2-Barcelona)
[Slides](https://www.dropbox.com/s/udxfsejpht96p2r/Investigation%20into%20influence%20of%20summer%20on%20RTAs%20in%20city%20of%20Barcelona.pdf?dl=0)
[Trello](https://trello.com/b/G0laRJKn/accidents-in-barcelona)
[Raw Data Sat](https://opendata-ajuntament.barcelona.cat/data/dataset/317e3743-fb79-4d2f-a128-5f12d2c9a55a/resource/6e2daeb5-e359-43ad-b0b5-7fdf438c8d6f/download/2018_accidents_vehicles_gu_bcn_.csv)
Loading