从连接的SQLAlchemy查询构造数据框

时间:2015-05-22 19:57:47

标签: python sqlalchemy

来自查询的

 session.query(Record, RecordMeasurement).filter(Record.record_pkid==RecordMeasurement.record_fkid)

我想构建1个数据框,其中包含Record列的值:

['datetime', 'record_number']

和RecordMeasurement列:

['power', 'voltage', 'temperature']

数据帧列顺序无关紧要。

1 个答案:

答案 0 :(得分:3)

将您的查询更改为只包含您需要的列:

query = (session.query(
    Record.datetime, Record.record_number, 
    RecordMeasurement.power, RecordMeasurement.voltage, RecordMeasurement.temperature,
    )
    .filter(Record.record_pkid==RecordMeasurement.record_fkid)
)

然后致电:

import pandas as pd
# ...
df = pd.read_sql(query.statement, query.session.bind)