โหลดข้อมูลไปยัง Postgres database
เมื่อดึงข้อมูลจากระบบต้นทางและแปลงข้อมูลให้พร้อมใช้งานสำหรับการวิเคราะห์หรือการรายงานแล้ว ขั้นตอนต่อไปคือการโหลดข้อมูลไปยังที่จัดเก็บปลายทาง การบันทึกข้อมูลที่ผ่านการทำความสะอาดแล้วลงใน SQL database ช่วยให้ผู้ใช้ข้อมูลเข้าถึงและรันคิวรีได้ง่ายขึ้น ในแบบฝึกหัดนี้ จะได้ฝึกโหลดข้อมูลที่ผ่านการทำความสะอาดแล้วไปยัง Postgres database
นำเข้า sqlalchemy เรียบร้อยแล้ว และ pandas พร้อมใช้งานในชื่อ pd ด้านล่างแสดงแถวแรกๆ ของ DataFrame cleaned_testing_scores:
street_address city math_score ... best_score
01M539 111 Columbia Street Manhattan 657.0 Math
02M545 350 Grand Street Manhattan 613.0 Math
01M292 220 Henry Street Manhattan 410.0 Math
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
ETL และ ELT ด้วย Python
คำแนะนำการฝึกหัด
- อัปเดต connection string เพื่อเชื่อมต่อกับ database
schoolsแล้วสร้าง connection object โดยใช้sqlalchemy - ใช้
pandasเพื่อเขียน DataFramecleaned_testing_scoresลงในตารางscoresใน databaseschools - หากตารางมีข้อมูลอยู่แล้ว ให้แทนที่ข้อมูลเดิมด้วย DataFrame ปัจจุบัน
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Update the connection string, create the connection object to the schools database
db_engine = sqlalchemy.____("postgresql+psycopg2://repl:password@localhost:5432/____")
# Write the DataFrame to the scores table
cleaned_testing_scores.____(
name="____",
con=db_engine,
index=False,
if_exists="____"
)