如何将Sqlalchemy表对象转换为Pandas DataFrame?

时间:2014-08-12 12:37:54

标签: python pandas sqlalchemy

是否可以将检索到的SqlAlchemy表对象转换为Pandas DataFrame,还是需要为此目标编写特定函数?

2 个答案:

答案 0 :(得分:5)

我想我之前已尝试过这个。这很糟糕,但对于整表ORM查询结果,这应该有效:

import pandas as pd

cols = [c.name for c in SQLA_Table.__table__.columns]
pk = [c.name for c in SQLA_Table.__table__.primary_key]
tuplefied_list = [(getattr(item, col) for col in cols) for item in result_list]

df = pd.DataFrame.from_records(tuplefied_list, index=pk, columns=cols)

部分查询结果(NamedTuples)也可以使用,但您必须构建DataFrame columnsindex以匹配您的查询。

答案 1 :(得分:0)

这可能不是最有效的方法,但是它对我来说非常有用,可以使用automap_base反映数据库表,然后将其转换为Pandas DataFrame。

import pandas as pd
from sqlalchemy.ext.automap import automap_base
from sqlalchemy import create_engine
from sqlalchemy.orm import Session

connection_string = "your:db:connection:string:here"
engine = create_engine(connection_string, echo=False)
session = Session(engine)

# sqlalchemy: Reflect the tables
Base = automap_base()
Base.prepare(engine, reflect=True)

# Mapped classes are now created with names by default matching that of the table name.
Table_Name = Base.classes.table_name

# Example query with filtering
query = session.query(Table_Name).filter(Table_Name.language != 'english')

# Convert to DataFrame
df = pd.read_sql(query.statement, engine)
df.head()