-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_db.py
More file actions
40 lines (30 loc) · 706 Bytes
/
Copy pathsetup_db.py
File metadata and controls
40 lines (30 loc) · 706 Bytes
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
import pandas as pd
import sqlite3
# CSV file name
FILE = "U.S._Chronic_Disease_Indicators.csv"
# SQLite database name
DB = "health.db"
print("Loading dataset into pandas...")
# Read CSV
df = pd.read_csv(FILE, low_memory=False)
# Clean column names
df.columns = [
c.strip().lower().replace(" ", "_")
for c in df.columns
]
print("Creating SQLite database...")
# Connect to SQLite
conn = sqlite3.connect(DB)
# Save dataframe into SQL table
df.to_sql(
"health_indicators",
conn,
if_exists="replace",
index=False
)
# Close connection
conn.close()
print(f"Loaded {len(df):,} rows into health.db")
print("Table name: health_indicators")
print("Columns:")
print(list(df.columns))