Skip to content

Latest commit

Β 

History

History
166 lines (107 loc) Β· 4.09 KB

File metadata and controls

166 lines (107 loc) Β· 4.09 KB

🚦 US Traffic Accidents: ETL & PostgreSQL Analysis Pipeline

A complete data engineering and analytics project using the US Accidents dataset, covering extraction, transformation, and loading into a PostgreSQL database, followed by SQL-based analysis and Python-powered visualizations.


πŸ“ Project Structure

TrafficAccidents-ETL-Analysis/
β”œβ”€β”€ data/                      # Raw dataset (ignored in Git)
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ create_schema.sql      # Table schema for PostgreSQL
β”‚   └── insert_data.py         # CSV-to-Postgres script
β”œβ”€β”€ scripts/
β”‚   └── clean_transform.py     # ETL transformation script
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ exploratory_analysis.ipynb  # All 10 visual plots
β”‚   └── advanced_queries.ipynb     # Raw SQL insights
β”œβ”€β”€ visualizations/            # Saved plots
β”œβ”€β”€ main.py                    # Runs the full ETL pipeline
β”œβ”€β”€ requirements.txt           # Python dependencies
β”œβ”€β”€ .env                       # DB credentials (excluded from Git)
└── README.md                  # Project overview

πŸ—ƒοΈ Dataset

  • Source: Kaggle: US Accidents (2016–2023)

  • Size: 7+ million rows

  • Fields include:

    • Start/End Time
    • Severity
    • City, State
    • Weather Condition
    • Visibility, Temperature, Wind Speed
    • Accident Duration (calculated)

πŸ§ͺ ETL Pipeline

  • πŸ“₯ Extract: CSV loaded using Pandas
  • 🧹 Transform: Cleaned nulls, calculated accident duration, converted timestamps
  • πŸ—οΈ Load: Inserted into PostgreSQL using SQLAlchemy

Run the full pipeline:

python main.py

πŸ“Š Exploratory Visualizations

Stored in the notebooks/visualizations/ folder and generated via Jupyter:

  1. Accidents by State

    Accidents by State

  2. Severity Distribution

    Severity Distribution

  3. Accident Duration Histogram

    Accident Duration Histogram

  4. Top Cities by Accidents

    Top Cities by Accidents

  5. Weather Conditions by State

    Weather Conditions by State

  6. Hourly Accident Distribution

    Hourly Accident Distribution

  7. Weekday vs Weekend Accidents

    Weekday vs Weekend Accidents

  8. Monthly Average Duration

    Monthly Average Duration

  9. Severity by Weather Condition

    Severity by Weather Condition

  10. Wind Speed vs Severity

    Wind Speed vs Severity


🧠 Deeper SQL Queries

Found in notebooks/advanced_queries.ipynb:

  • Top cities with most accidents
  • Average duration per state
  • Severity Γ— Weather correlation
  • Time-based aggregations (hour, weekday, month)
  • Weather/visibility/wind effect on severity

πŸ› οΈ Tech Stack

  • Language: Python 3.10+
  • Database: PostgreSQL
  • Libraries: Pandas, SQLAlchemy, Matplotlib, Seaborn, Dotenv
  • Notebook: Jupyter

πŸ“¦ Setup Instructions

  1. Clone repo:

    git clone https://github.com/ChaiiShree/sql.git
    cd TrafficAccidents-ETL-Analysis
  2. Create .env file:

    DATABASE_URL=postgresql://username:password@localhost:5432/dbname
    
  3. Set up environment:

    python -m venv venv
    venv/Scripts/activate  # On Windows
    pip install -r requirements.txt
  4. Download dataset from Kaggle into data/

  5. Run ETL:

    python main.py
  6. Launch notebooks:

    jupyter notebook

πŸ“„ License

This project is licensed under the MIT License. Dataset credit: Β© Kaggle – US Accidents