SQLAlchemy ORM 转换为 Pandas DataFrame
在本文中,我们将看到如何使用Python将 SQLAlchemy ORM 转换为 Pandas DataFrame。
我们需要在Python环境中安装 sqlalchemy 和 pandas 库——
$ pip install sqlalchemy
$ pip install pandas
对于我们的示例,我们将使用 MySQL 数据库,其中我们已经创建了一个名为 students 的表。您可以自由使用任何数据库,但您需要相应地创建其连接字符串。下面提供了此示例的原始 SQL 脚本供参考:
CREATE DATABASE Geeks4Geeks;
USE Geeks4Geeks;
CREATE TABLE students (
first_name VARCHAR(50),
last_name VARCHAR(50),
course VARCHAR(50),
score FLOAT
);
INSERT INTO students VALUES
('Ashish', 'Mysterio', 'Statistics', 96),
('Rahul', 'Kumar', 'Statistics', 83),
('Irfan', 'Malik', 'Statistics', 66),
('Irfan', 'Ahmed', 'Statistics', 81),
('John', 'Wick', 'Statistics', 77),
('Mayon', 'Irani', 'Statistics', 55),
('Ashish', 'Mysterio', 'Sociology', 85),
('Rahul', 'Kumar', 'Sociology', 78),
('Irfan', 'Malik', 'Biology', 92),
('Irfan', 'Ahmed', 'Chemistry', 45),
('John', 'Wick', 'Biology', 78),
('Mayon', 'Irani', 'Physics', 78);
SELECT * FROM students;
将 SQLAlchemy ORM 转换为 pandas 数据帧的语法与原始 SQL 查询的语法相同,如下所示:
Syntax: pandas.read_sql(sql, con, **kwargs)
Where:
- sql: The SELECT SQL statement to be executed
- con: SQLAlchemy engine object to establish a connection to the database
请注意,您也可以使用pandas.read_sql_query()代替pandas.read_sql()
示例 1:
在上面的示例中,我们可以看到pandas.read_sql()方法的sql参数接受了 SQLAlchemy ORM 查询,因为我们可能在没有进行 pandas 数据帧转换的情况下定义了它。 db.select()将在read_sql()方法读取时转换为原始 SQL 查询。在输出中,我们还打印了响应对象的类型。输出是一个 pandas DataFrame 对象,我们在其中获取了学生表中存在的所有记录。
Python3
import pandas
import sqlalchemy as db
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# DEFINE THE ENGINE (CONNECTION OBJECT)
engine = db.create_engine("mysql+pymysql:\
//root:password@localhost/Geeks4Geeks")
# CREATE THE TABLE MODEL TO USE IT FOR QUERYING
class Students(Base):
__tablename__ = 'students'
first_name = db.Column(db.String(50),
primary_key=True)
last_name = db.Column(db.String(50),
primary_key=True)
course = db.Column(db.String(50))
score = db.Column(db.Float)
# SQLAlCHEMY ORM QUERY TO FETCH ALL RECORDS
df = pandas.read_sql_query(
sql = db.select([Students.first_name,
Students.last_name,
Students.course,
Students.score]),
con = engine
)
print("Type:", type(df))
print()
print(df)
Python3
import pandas
import sqlalchemy as db
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# DEFINE THE ENGINE (CONNECTIO OBJECT)
engine = db.create_engine("mysql+pymysql:\
//root:password@localhost/Geeks4Geeks")
# CREATE THE TABLE MODEL TO USE IT FOR QUERYING
class Students(Base):
__tablename__ = 'students'
first_name = db.Column(db.String(50),
primary_key=True)
last_name = db.Column(db.String(50),
primary_key=True)
course = db.Column(db.String(50))
score = db.Column(db.Float)
# CREATE A SESSION OBJECT TO INITIATE QUERY IN DATABASE
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind = engine)
session = Session()
# SQLAlCHEMY ORM QUERY TO FETCH ALL RECORDS
df = pandas.read_sql_query(
sql = session.query(Students.first_name,
Students.last_name).filter(
Students.score > 80).statement,
con = engine
)
print("Type:", type(df))
print()
print(df)
输出:
示例 2:
在这个例子中,我们使用了会话对象来绑定连接引擎。我们还应用了一个 filter() 方法,它等效于 SQL 中的 WHERE 子句。它包含的条件是选择的记录应该包含那些得分大于 80 的学生的名字和姓氏。这里值得注意的是,对于使用会话对象构建的查询,我们需要使用语句属性显式转换为原始 SQL 查询。这是必需的,因为如果没有 statement 属性,它将是一个无法由pandas.read_sql()方法执行的sqlalchemy.orm.query.Query对象,并且您将收到sqlalchemy.exc.ObjectNotExecutableError错误。上面的输出显示了作为 pandas DataFrame 的对象的类型以及响应。
Python3
import pandas
import sqlalchemy as db
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# DEFINE THE ENGINE (CONNECTIO OBJECT)
engine = db.create_engine("mysql+pymysql:\
//root:password@localhost/Geeks4Geeks")
# CREATE THE TABLE MODEL TO USE IT FOR QUERYING
class Students(Base):
__tablename__ = 'students'
first_name = db.Column(db.String(50),
primary_key=True)
last_name = db.Column(db.String(50),
primary_key=True)
course = db.Column(db.String(50))
score = db.Column(db.Float)
# CREATE A SESSION OBJECT TO INITIATE QUERY IN DATABASE
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind = engine)
session = Session()
# SQLAlCHEMY ORM QUERY TO FETCH ALL RECORDS
df = pandas.read_sql_query(
sql = session.query(Students.first_name,
Students.last_name).filter(
Students.score > 80).statement,
con = engine
)
print("Type:", type(df))
print()
print(df)
输出: