-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableInsertion.py
161 lines (117 loc) · 4.84 KB
/
TableInsertion.py
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
# Insert data into each of 3 exam tables
#import libraries
import pandas as pd
import os
from sqlalchemy import create_engine, MetaData, Table, select, insert
###########################################################################################
# testing insertion with table from udemy course
# create engine
engine = create_engine('postgresql+psycopg2://postgres:password@localhost:####/Test Database')
# connection
conn = engine.connect()
metadata = MetaData()
sql_table = Table('customers', metadata, autoload=True, autoload_with=engine)
sql_columns=sql_table.columns.keys()
results = conn.execute(select([sql_table])).fetchall()
df = pd.DataFrame(results, columns=sql_table.columns.keys())
df_columns=reversed(df.columns.tolist())
#array=list(i for i in range (6,8))
array=list(i for i in range (8,9))
# create sample rows and add them to a list for insertion
rows=[]
for i in array:
row=['max',i,'duke']
rows.append(dict(zip(df_columns,row)))
#insert rows into table
conn.execute(sql_table.insert(),rows)
#check results of insertion
results = conn.execute(select([sql_table])).fetchall()
df_check = pd.DataFrame(results, columns=sql_table.columns.keys())
df_check.shape # equaled 7 so checks out
###########################################################################
# now for the real thing
def load_postgres(table_name):
# create engine
engine = create_engine('postgresql+psycopg2://postgres:password@localhost:####/Regents Exams DataBase')
# connection
conn = engine.connect()
metadata = MetaData()
sql_table = Table(table_name, metadata, autoload=True, autoload_with=engine)
# assign query to a variable with python sqlalchemy select
results = conn.execute(select([sql_table])).fetchall()
df = pd.DataFrame(results, columns=sql_table.columns.keys())
return df
os.chdir('D:\\MathRegentsDataFiles')
alg_csv=pd.read_csv('PreppedAlg1QuestionBreakdown.csv', encoding='latin1')
alg_2018=alg_csv[alg_csv.DateFixed=='18-Jun']
alg=load_postgres('Alg1CC')
# create engine
engine = create_engine('postgresql+psycopg2://postgres:password@localhost:####/Regents Exams DataBase')
# connection
conn = engine.connect()
metadata = MetaData()
sql_table = Table('Alg1CC', metadata, autoload=True, autoload_with=engine)
sql_columns=sql_table.columns.keys()
rows = []
for i in range(alg.shape[0]+1,alg_csv.shape+1):
row=alg_2018.iloc[i-alg.shape[0]+1,:].values.tolist()
row=[str(x) for x in row]
row.insert(0, str(i))
rows.append(dict(zip(sql_columns,row)))
# insert data into table
conn.execute(sql_table.insert(),rows)
# check results of insertion
results = conn.execute(select([sql_table])).fetchall()
df_check = pd.DataFrame(results, columns=sql_table.columns.keys())
df_check.shape
# successful
##############################################################################################
#geometry
geo_csv=pd.read_csv('PreppedGeoQuestionBreakdown.csv', encoding='latin1')
geo_2018=geo_csv[geo_csv.DateFixed=='18-Jun']
geo=load_postgres('Geometry')
geo_2018.loc[:,'DateFixed']='Jun-18'
# create engine
engine = create_engine('postgresql+psycopg2://postgres:password@localhost:####/Regents Exams DataBase')
# connection
conn = engine.connect()
metadata = MetaData()
sql_table = Table('Geometry', metadata, autoload=True, autoload_with=engine)
sql_columns=sql_table.columns.keys()
rows = []
for i in range(324,359):
row=geo_2018.iloc[i-324,:].values.tolist()
row=[str(x) for x in row]
row.insert(0, str(i))
rows.append(dict(zip(sql_columns,row)))
# insert data into table
conn.execute(sql_table.insert(),rows)
# check results of insertion
results = conn.execute(select([sql_table])).fetchall()
df_check = pd.DataFrame(results, columns=sql_table.columns.keys())
df_check.shape
#################################################################################################
# Algebra 2
algt_csv=pd.read_csv('PreppedAlg2QuestionBreakdown.csv', encoding='latin1')
algt_2018=algt_csv[algt_csv.DateFixed=='Jun-18']
algt=load_postgres('Alg2CC')
# create engine
engine = create_engine('postgresql+psycopg2://postgres:password@localhost:####/Regents Exams DataBase')
# connection
conn = engine.connect()
metadata = MetaData()
sql_table = Table('Alg2CC', metadata, autoload=True, autoload_with=engine)
sql_columns=sql_table.columns.keys()
rows = []
for i in range(223,260):
row=algt_2018.iloc[i-223,:].values.tolist()
row=[str(x) for x in row]
row.insert(0, str(i))
rows.append(dict(zip(sql_columns,row)))
# insert data into table
conn.execute(sql_table.insert(),rows)
# check results of insertion
results = conn.execute(select([sql_table])).fetchall()
df_check = pd.DataFrame(results, columns=sql_table.columns.keys())
df_check.shape
# successful