In [1]:
%load_ext sql

In [2]:
%sql sqlite:///smaller.db


Out[2]:
u'Connected: None@smaller.db'

In [3]:
%sql select * from data limit 5


Done.
Out[3]:
state facility_name orispl_code unitid op_date op_hour op_time gload SO2_MASS NOX_MASS CO2_MASS HEAT_INPUT state:1 facility_name:1 orispl_code:1 unitid:1 year county latitude longitude fuel_type fule_type_secondary fuel_actual
WA Centralia 3845 BW21 01-01-2001 0 1.0 704.0 11493.6 2506.854 720.5 7022.0 WA Centralia 3845 BW21 2001 Lewis 46.7559 -122.8578 Coal Coal
WA Centralia 3845 BW21 01-01-2001 1 1.0 706.0 11774.7 2577.369 736.6 7179.3 WA Centralia 3845 BW21 2001 Lewis 46.7559 -122.8578 Coal Coal
WA Centralia 3845 BW21 01-01-2001 2 1.0 704.0 11156.9 2606.306 718.8 7006.2 WA Centralia 3845 BW21 2001 Lewis 46.7559 -122.8578 Coal Coal
WA Centralia 3845 BW21 01-01-2001 3 1.0 702.0 10990.0 2561.697 716.2 6980.1 WA Centralia 3845 BW21 2001 Lewis 46.7559 -122.8578 Coal Coal
WA Centralia 3845 BW21 01-01-2001 4 1.0 702.0 11027.6 2631.762 716.2 6980.8 WA Centralia 3845 BW21 2001 Lewis 46.7559 -122.8578 Coal Coal

In [ ]:
%sql select count(*) from data

In [3]:
%sql select * from data where state is UPPER('wa') limit 5


Done.
Out[3]:
state name orispl_code unitid op_date op_hour op_time gload SO2_MASS NOX_MASS CO2_MASS heat_input county latitude longitude fuel_type
WA Centralia 3845 BW21 01-01-2001 1 1.0 706.0 11774.7 2577.369 736.6 7179.3 Lewis 46.7559 -122.8578 Coal
WA Centralia 3845 BW21 01-01-2001 2 1.0 704.0 11156.9 2606.306 718.8 7006.2 Lewis 46.7559 -122.8578 Coal
WA Centralia 3845 BW21 01-01-2001 3 1.0 702.0 10990.0 2561.697 716.2 6980.1 Lewis 46.7559 -122.8578 Coal
WA Centralia 3845 BW21 01-01-2001 4 1.0 702.0 11027.6 2631.762 716.2 6980.8 Lewis 46.7559 -122.8578 Coal
WA Centralia 3845 BW21 01-01-2001 5 1.0 699.0 11258.5 2678.697 721.4 7030.7 Lewis 46.7559 -122.8578 Coal

In [19]:
import sqlite3
for state in ['or', 'ca', 'id', 'nv', 'ut', 'az', 'nm', 'co', 'wy', 'mt', 'tx']:
    con = sqlite3.connect(state + ".db")
    con = sqlite3.connect("smaller.db")
    cur = con.cursor()
    cur.execute("attach database '" + state + ".db' AS " + state + "db;")
    cur.execute("create table " + state + "db.data as \
    select * from data where data.state = UPPER('" + state + "');")

In [16]:



Done.
Out[16]:
[]

In [17]:
%sql create table wash.data as select * from data where state is UPPER('wa');


Done.
Out[17]:
[]

In [21]:
%%sql
ATTACH DATABASE '2016.db' AS other;


Done.
Out[21]:
[]

In [22]:
%sql create table other.data as select * from data where substr(op_date, -4) is '2016';


Done.
Out[22]:
[]

In [ ]:
import sqlite3
con = sqlite3.connect("smaller.db")
for year in range(2001, 2017):
    con = sqlite3.connect(str(year) + ".db")
    cur = con.cursor()
    cur.execute("drop table if exists data;")
    con = sqlite3.connect("smaller.db")
    cur = con.cursor()
    cur.execute("attach database '" + str(year) + ".db' AS db" + str(year) + ";")
    cur.execute("create table db" + str(year) + ".data as \
    select * from data where substr(op_date, -4) is cast(" + str(year) + " as varchar);")

In [29]:
%%sql
ATTACH DATABASE 'intermountain.db' AS jyoung;
create table jyoung.data as select *, substr(op_date, -4) as year from data where name is 'Intermountain';


Done.
Done.
Out[29]:
[]

In [31]:
import sqlite3
import pandas
con = sqlite3.connect('intermountain.db')
table = pandas.read_sql("select unitid, op_date, op_hour, op_time, gload, SO2_MASS, NOX_MASS, CO2_MASS, heat_input, year from data", con)
table.to_csv('intermountain.csv')

In [ ]: