我在 pandas DataFrame 中有一些基本信息。我需要将它与我可以通过 pyodbc 连接访问的一些参考表连接起来。有没有办法将 sql 结果集放入 pandas DataFrame 而无需先将结果集写入 csv?
将这个额外的步骤转移到 csv 并进入 DataFrame 似乎是一种浪费。
I've gotten pyodbc
to work with my SQL Server
instance, then, with some help from this thread, I got the sql return to load a dataframe.
I already setup a pyodbc
connection, then made a call to it.
import pyodbc
import pandas.io.sql as psql
cnxn = pyodbc.connect(your_connection_info)
cursor = cnxn.cursor()
sql = ("""SELECT * FROM Source""")
df = psql.frame_query(sql, cnxn)
cnxn.close()
df
should return your dataframe now. The hardest part for me was getting pyodbc
up and running - I had to use freetds
and it took a lot of trial and error to get it work.