Nạp dữ liệu gọn nhẹ với pandas
Amany Mahfouz
Instructor






create_engine() của sqlalchemy tạo engine quản lý kết nối cơ sở dữ liệusqlite:///filename.dbpd.read_sql(query, engine) để nạp dữ liệu từ cơ sở dữ liệuquery: Chuỗi chứa truy vấn SQL hoặc tên bảng cần nạpengine: Đối tượng engine/kết nối cơ sở dữ liệuSELECT [column_names] FROM [table_name];
SELECT * FROM [table_name];
# Nạp pandas và create_engine của sqlalchemy import pandas as pd from sqlalchemy import create_engine# Tạo engine cơ sở dữ liệu để quản lý kết nối engine = create_engine("sqlite:///data.db")# Nạp toàn bộ bảng weather bằng tên bảng weather = pd.read_sql("weather", engine)
# Tạo engine cơ sở dữ liệu để quản lý kết nối engine = create_engine("sqlite:///data.db")# Nạp toàn bộ bảng weather bằng SQL weather = pd.read_sql("SELECT * FROM weather", engine)print(weather.head())
station name latitude ... prcp snow tavg tmax tmin
0 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 52 42
1 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 48 39
2 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 48 42
3 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 51 40
4 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.75 0.0 61 50
[5 rows x 13 columns]
Nạp dữ liệu gọn nhẹ với pandas