first commit

This commit is contained in:
2024-08-09 15:39:27 -07:00
commit dce89338af
8 changed files with 12149 additions and 0 deletions

8944
analysis/analysis2.html Normal file

File diff suppressed because one or more lines are too long

1382
analysis/analysis2.ipynb Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

BIN
data_setup/Spills.xlsx Normal file

Binary file not shown.

160
data_setup/create_csv.ipynb Normal file
View File

@@ -0,0 +1,160 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sqlalchemy import create_engine\n",
"import geopandas as gpd\n",
"\n",
"import os\n",
"\n",
"# Database connection details from zshrc environment variables\n",
"db_name = 'colorado_spills'\n",
"user = os.getenv('DB_USER')\n",
"password = os.getenv('DB_PASSWORD')\n",
"host = os.getenv('DB_HOST')\n",
"port = os.getenv('DB_PORT')\n",
"\n",
"\n",
"# Create an engine to connect to the PostgreSQL database\n",
"engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{db_name}')\n",
"\n",
"# Read in the spills_with_demographics as spills\n",
"spills = pd.read_sql_table('spills_with_demographics', engine)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<bound method NDFrame.head of Document # Report Operator \\\n",
"0 400628833 I w/S GREAT WESTERN OPERATING COMPANY LLC \n",
"1 400629910 I COLORADO OIL & GAS CONSERVATION COMMISSION \n",
"2 400629998 S COLORADO OIL & GAS CONSERVATION COMMISSION \n",
"3 400630821 I KERR MCGEE GATHERING LLC \n",
"4 400631216 I NOBLE ENERGY INC \n",
"... ... ... ... \n",
"16885 403825450 I w/S NOBLE ENERGY INC \n",
"16886 403825485 I w/S NOBLE ENERGY INC \n",
"16887 403825575 I w/S NOBLE ENERGY INC \n",
"16888 403826200 I NOBLE ENERGY INC \n",
"16889 403826294 I CAERUS PICEANCE LLC \n",
"\n",
" Operator # Tracking # Initial Report Date Date of Discovery \\\n",
"0 10110 400628833 06/18/2014 06/11/2014 \n",
"1 5 400629910 06/19/2014 06/14/2014 \n",
"2 5 400629910 06/19/2014 06/14/2014 \n",
"3 47121 400630821 06/20/2014 06/19/2014 \n",
"4 100322 400631216 06/23/2014 06/20/2014 \n",
"... ... ... ... ... \n",
"16885 100322 403825450 06/14/2024 06/13/2024 \n",
"16886 100322 403825485 06/14/2024 06/13/2024 \n",
"16887 100322 403825575 06/14/2024 06/13/2024 \n",
"16888 100322 403826200 06/14/2024 06/14/2024 \n",
"16889 10456 403826294 06/15/2024 06/15/2024 \n",
"\n",
" Spill Type Qtr Qtr Section ... total_population white_population \\\n",
"0 Historical SESE 2 ... 5491.0 4767.0 \n",
"1 Historical NWNE 1 ... 7335.0 6302.0 \n",
"2 Historical NWNE 1 ... 7335.0 6302.0 \n",
"3 Recent NWNW 15 ... 427.0 222.0 \n",
"4 Recent NENW 36 ... 3470.0 3105.0 \n",
"... ... ... ... ... ... ... \n",
"16885 Historical SWSE 7 ... 7335.0 6302.0 \n",
"16886 Historical SWSE 7 ... 7335.0 6302.0 \n",
"16887 Historical SWSE 7 ... 7335.0 6302.0 \n",
"16888 Recent SWSE 5 ... 4023.0 3581.0 \n",
"16889 Recent NESW 22 ... 7240.0 5646.0 \n",
"\n",
" hispanic_population median_household_income poverty_population \\\n",
"0 772.0 88286.0 325.0 \n",
"1 2011.0 71440.0 831.0 \n",
"2 2011.0 71440.0 831.0 \n",
"3 183.0 -666666666.0 0.0 \n",
"4 167.0 122604.0 129.0 \n",
"... ... ... ... \n",
"16885 2011.0 71440.0 831.0 \n",
"16886 2011.0 71440.0 831.0 \n",
"16887 2011.0 71440.0 831.0 \n",
"16888 612.0 82865.0 268.0 \n",
"16889 1659.0 64573.0 693.0 \n",
"\n",
" unemployed_population percent_white percent_hispanic percent_poverty \\\n",
"0 80.0 86.814788 14.059370 5.918776 \n",
"1 166.0 85.916837 27.416496 11.329243 \n",
"2 166.0 85.916837 27.416496 11.329243 \n",
"3 0.0 51.990632 42.857143 0.000000 \n",
"4 13.0 89.481268 4.812680 3.717579 \n",
"... ... ... ... ... \n",
"16885 166.0 85.916837 27.416496 11.329243 \n",
"16886 166.0 85.916837 27.416496 11.329243 \n",
"16887 166.0 85.916837 27.416496 11.329243 \n",
"16888 62.0 89.013174 15.212528 6.661695 \n",
"16889 240.0 77.983425 22.914365 9.571823 \n",
"\n",
" unemployment_rate \n",
"0 1.456930 \n",
"1 2.263122 \n",
"2 2.263122 \n",
"3 0.000000 \n",
"4 0.374640 \n",
"... ... \n",
"16885 2.263122 \n",
"16886 2.263122 \n",
"16887 2.263122 \n",
"16888 1.541138 \n",
"16889 3.314917 \n",
"\n",
"[16890 rows x 118 columns]>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spills.head"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Save the spills DataFrame as an Excel file\n",
"spills.to_excel(\"spills_with_demographics.xlsx\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "funkyfunk",
"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.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,86 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Database and table creation completed successfully!\n"
]
}
],
"source": [
"import pandas as pd\n",
"from sqlalchemy import create_engine\n",
"import psycopg2\n",
"\n",
"# Step 1: Load the Excel file into a DataFrame\n",
"file_path = '/home/dadams/Repos/colorado_spills/Spills.xlsx'\n",
"df = pd.read_excel(file_path)\n",
"\n",
"\n",
"# Step 2: Create PostgreSQL database and enable PostGIS\n",
"db_name = 'colorado_spills'\n",
"user = 'postgres'\n",
"password = 'MandyLinkToby3'\n",
"host = '192.168.0.74'\n",
"port = '5432'\n",
"\n",
"# Create a connection to PostgreSQL\n",
"engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/postgres')\n",
"\n",
"# Connect to PostgreSQL\n",
"conn = psycopg2.connect(dbname='postgres', user=user, password=password, host=host, port=port)\n",
"conn.autocommit = True\n",
"cur = conn.cursor()\n",
"\n",
"# Create database\n",
"cur.execute(f'CREATE DATABASE {db_name}')\n",
"cur.close()\n",
"conn.close()\n",
"\n",
"# Connect to the new database\n",
"engine = create_engine(f'postgresql+psycopg2://{user}:{password}@{host}:{port}/{db_name}')\n",
"conn = psycopg2.connect(dbname=db_name, user=user, password=password, host=host, port=port)\n",
"conn.autocommit = True\n",
"cur = conn.cursor()\n",
"\n",
"# Enable PostGIS extension\n",
"cur.execute('CREATE EXTENSION postgis')\n",
"cur.close()\n",
"conn.close()\n",
"\n",
"# Step 3: Load DataFrame into PostgreSQL table\n",
"table_name = 'spills'\n",
"df.to_sql(table_name, engine, if_exists='replace', index=False)\n",
"\n",
"print(\"Database and table creation completed successfully!\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "funkyfunk",
"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.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Binary file not shown.