-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_all_table.py
More file actions
29 lines (23 loc) · 783 Bytes
/
Copy pathload_all_table.py
File metadata and controls
29 lines (23 loc) · 783 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
import pandas as pd
from sqlalchemy import create_engine
from pathlib import Path
engine = create_engine("postgresql+psycopg2://postgres:Leodas@localhost:5432/credit_risk_db;")
data_path = Path(r"D:\ML PRoject\home_credit_data")
for csv_file in data_path.glob("*.csv"):
table_name = csv_file.stem.lower()
print(f"\n loading {table_name}")
try:
df = pd.read_csv(csv_file)
print(f"rows {len(df)}")
df.to_sql(
table_name,
engine,
if_exists = "replace",
index = False,
chunksize = 5000
)
print(f"{table_name} Loaded successfuly")
except Exception as e:
print(f"Error loading {table_name}: {e}")
print(e)
print("\n all tables loaded")