使用 pandas 高效导入数据
Amany Mahfouz
Instructor






sqlalchemy 的 create_engine() 创建处理数据库连接的引擎sqlite:///filename.dbpd.read_sql(query, engine) 从数据库加载数据query:要运行的 SQL 查询字符串或要加载的表名engine:连接/数据库引擎对象SELECT [column_names] FROM [table_name];
SELECT * FROM [table_name];
# 加载 pandas 和 sqlalchemy 的 create_engine import pandas as pd from sqlalchemy import create_engine# 创建数据库引擎以管理连接 engine = create_engine("sqlite:///data.db")# 通过表名加载整个 weather 表 weather = pd.read_sql("weather", engine)
# 创建数据库引擎以管理连接 engine = create_engine("sqlite:///data.db")# 用 SQL 加载整个 weather 表 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]
使用 pandas 高效导入数据