-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject3-Group7.draft1.py
More file actions
187 lines (142 loc) · 6.49 KB
/
Copy pathProject3-Group7.draft1.py
File metadata and controls
187 lines (142 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy import DateTime, Boolean
from sqlalchemy import exists
from sqlalchemy import sql, select, join, desc
import csv
import pandas as pd
import datetime
# Create a sqlite database
engine = create_engine('sqlite:///project3group7.sqlite')
metadata = MetaData(engine)
# Create the Airports Table
try:
Airports = Table('Airports', metadata, autoload=True)
except:
Airports = Table ('Airports', metadata,
Column('ID', Integer, autoincrement=True),
Column('Code', String, primary_key=True),
Column('City', String),
Column('State', String)
)
# Create the Flights table
try:
Flights = Table('Flights', metadata, autoload=True)
except:
Flights = Table ('Flights', metadata,
Column('ID', Integer, autoincrement=True),
Column('Time', String, primary_key=True),
Column('Fl_date', DateTime),
Column('Airline_ID', String),
Column('Origin', String, ForeignKey("Airports.Code")),
Column('Destination', String),
Column('Dep_Time', String),
Column('Dep_Delay_New', Integer),
Column('Dep_Del15', Integer),
Column('Arr_Time', String),
Column('Arr_Delay_New', Integer),
Column('Arr_Del15', Integer),
Column('Cancelled', Boolean),
Column('Cancellation_Code', String),
Column('Diverted', Boolean),
Column('Air_Time', Integer),
Column('Flights', Integer),
Column('Distance', Integer),
Column('Carrier_Delay', Integer),
Column('Weather_Delay', Integer),
Column('NAS_Delay', Integer),
Column('Security_Delay', Integer),
Column('Late_Aircraft_Delay', Integer)
)
metadata.create_all(engine)
# Read the csv file
flights = open("flights.1K.csv")
reader = csv.DictReader(flights)
Airport_dict = {}
# Create a dictionary of unique airport codes
for Line in reader:
if Line['ORIGIN'] not in Airport_dict:
Airport_dict[Line['ORIGIN']]=[Line['ORIGIN_CITY_NAME'], Line['ORIGIN_STATE_ABR']]
if Line['DEST'] not in Airport_dict:
Airport_dict[Line['DEST']]=[Line['DEST_CITY_NAME'], Line['DEST_STATE_ABR']]
conn = engine.connect()
# Add the Airport_dict codes to the Airports table
def insert_airport(code,city,state):
ins = Airports.insert().values(Code=code,
City=city,
State=state)
result = conn.execute(ins)
for key, value in Airport_dict.items():
insert_airport(key, value[0], value[1])
# Close the file
flights.close()
flights = open("flights.1K.csv")
reader = csv.DictReader(flights)
Flight_dict = {}
def to_date(dates, lookup=False, **args):
if lookup:
return dates.map({v: pd.to_datetime(v, **args) for v in dates.unique()})
return pd.to_datetime(dates, **args)
# Create a dictionary of Time using the departure time and arrival time
# Add data to the database
for Line in reader:
Time = (Line['DEP_TIME'] + Line['ARR_TIME'])
if (Line['DEP_TIME'] + Line['ARR_TIME']) not in Flight_dict:
Flight_dict[Time]=[to_date(Line['FL_DATE']), Line['AIRLINE_ID'], Line['ORIGIN'], Line['DEST'],
Line['DEP_TIME'], Line['DEP_DELAY_NEW'], Line['DEP_DEL15'],
Line['ARR_TIME'], Line['ARR_DELAY_NEW'], Line['ARR_DEL15'],
int(float((Line['CANCELLED']))), Line['CANCELLATION_CODE'], int(float((Line['DIVERTED']))), Line['AIR_TIME'], Line['FLIGHTS'],
Line['DISTANCE'], Line['CARRIER_DELAY'], Line['WEATHER_DELAY'], Line['NAS_DELAY'], Line['SECURITY_DELAY'], Line['LATE_AIRCRAFT_DELAY']]
conn = engine.connect()
def insert_flight(time, fl_date, airline_id, origin, destination, dep_time, dep_delay_new,
dep_del15, arr_time, arr_delay_new, arr_del15, cancelled, cancellation_code,
diverted, air_time, flights, distance, carrier_delay, weather_delay,
nas_delay, security_delay, late_aircraft_delay):
ins = Flights.insert().values(Time = time,
Fl_date = fl_date,
Airline_ID = airline_id,
Origin = origin,
Destination = destination,
Dep_Time = dep_time,
Dep_Delay_New = dep_delay_new,
Dep_Del15 = dep_del15,
Arr_Time = arr_time,
Arr_Delay_New = arr_delay_new,
Arr_Del15 = arr_del15,
Cancelled = cancelled,
Cancellation_Code = cancellation_code,
Diverted = diverted,
Air_Time = air_time,
Flights = flights,
Distance = distance,
Carrier_Delay = carrier_delay,
Weather_Delay = weather_delay,
NAS_Delay = nas_delay,
Security_Delay = security_delay,
Late_Aircraft_Delay = late_aircraft_delay)
result = conn.execute(ins)
for key, value in Flight_dict.items():
insert_flight(key, value[0], value[1], value[2], value[3], value[4], value[5], value[6],
value[7], value[8], value[9], value[10], value[11], value[12], value[13],
value[14], value[15], value[16], value[17], value[18], value[19], value[20])
# Query 1 for select
# Select 25 flights with limit function and show their time for departure and arrival
Query1 = select([Flights.c.Dep_Time, Flights.c.Arr_Time]).limit(25)
result1 = conn.execute(Query1)
for row in result1:
print(row)
# Query 2 for where
# Show the Airline ID, Origin Airport ID and Destination Airport ID for short-distance(within 200 miles) flights
Query2 = select([Flights.c.Airline_ID, Flights.c.Origin, Flights.c.Destination]).where(Flights.c.Distance < 200)
result2 = conn.execute(Query2)
for row in result2:
print(row)
# Query 3 for join
# Join the same Airport Code from both the Destination in Flights table and Code in Airports table, and show the airline ID, oriin airport code, destination airport code, city name and state name
join_obj = Flights.join(Airports,
Flights.c.Destination==Airports.c.Code)
Query3 = select(
[Flights.c.Airline_ID, Flights.c.Origin, Flights.c.Destination, Airports.c.City, Airports.c.State]).select_from(join_obj)
result3 = conn.execute(Query3)
for row in result3:
print(row)