-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
77 lines (63 loc) · 1.89 KB
/
database.py
File metadata and controls
77 lines (63 loc) · 1.89 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
import sqlite3
import pandas as pd
import os
# Database file path
DB_FILE = "healthnav.db"
CSV_FILE = "MI_Doctors.csv" # Ensure this CSV is in the same directory
def create_database():
if os.path.exists(DB_FILE):
os.remove(DB_FILE)
"""Creates the SQLite database and required tables."""
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
# Create Users Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER,
gender TEXT,
insurance TEXT,
street TEXT,
city TEXT,
state TEXT,
zip TEXT
)
''')
# Create Doctors Table
cursor.execute('''
CREATE TABLE IF NOT EXISTS doctors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
row_id INTEGER,
npi TEXT,
last_name TEXT,
first_name TEXT,
degree TEXT,
street_address_1 TEXT,
street_address_2 TEXT,
city TEXT,
state TEXT,
zip_code TEXT,
country TEXT,
phone TEXT,
gender TEXT,
specialty TEXT
)
''')
conn.commit()
conn.close()
def load_doctors_data():
conn = sqlite3.connect(DB_FILE)
cursor = conn.cursor()
df = pd.read_csv(CSV_FILE, dtype={"zip_code": str}, low_memory=False)
df.rename(columns={"Unnamed: 0": "row_id", "NPI": "npi"}, inplace=True)
df.columns = [col.lower().replace(" ", "_") for col in df.columns]
df['zip_code'] = df['zip_code'].astype(str)
df['phone'] = df['phone'].astype(str)
df.to_sql("doctors", conn, if_exists="replace", index=False)
conn.commit()
conn.close()
if __name__ == "__main__":
create_database()
load_doctors_data()
print("Database initialized successfully.")